瀏覽代碼

Rename context to example to make it more explicit, context will be a local command managing contexts (similar to docker-cli context command, adapted here for more generic purpose)

Guillaume Tardif 5 年之前
父節點
當前提交
91951eb03a
共有 4 個文件被更改,包括 122 次插入79 次删除
  1. 1 0
      .gitignore
  2. 2 79
      cmd/context.go
  3. 107 0
      cmd/example.go
  4. 12 0
      cmd/main.go

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 bin/
 bin/
+.idea/

+ 2 - 79
cmd/context.go

@@ -28,92 +28,15 @@
 package main
 package main
 
 
 import (
 import (
-	"context"
-	"encoding/json"
-	"fmt"
-	"os"
-	"os/exec"
-	"path/filepath"
-	"time"
-
-	"github.com/docker/api/client"
-	"github.com/gogo/protobuf/types"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 	"github.com/urfave/cli"
 	"github.com/urfave/cli"
 )
 )
 
 
-func init() {
-	// initial hack to get the path of the project's bin dir
-	// into the env of this cli for development
-
-	path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/docker/api/bin")
-	if err := os.Setenv("PATH", fmt.Sprintf("$PATH:%s", path)); err != nil {
-		panic(err)
-	}
-}
-
 var contextCommand = cli.Command{
 var contextCommand = cli.Command{
 	Name:  "context",
 	Name:  "context",
 	Usage: "manage contexts",
 	Usage: "manage contexts",
 	Action: func(clix *cli.Context) error {
 	Action: func(clix *cli.Context) error {
 		// return information for the current context
 		// return information for the current context
-		ctx, cancel := client.NewContext()
-		defer cancel()
-
-		// get our current context
-		ctx = current(ctx)
-
-		client, err := connect(ctx)
-		if err != nil {
-			return errors.Wrap(err, "cannot connect to backend")
-		}
-		defer client.Close()
-
-		info, err := client.BackendInformation(ctx, &types.Empty{})
-		if err != nil {
-			return errors.Wrap(err, "fetch backend information")
-		}
-		enc := json.NewEncoder(os.Stdout)
-		enc.SetIndent("", " ")
-		return enc.Encode(info)
+		return errors.New("Error : To be implemented")
 	},
 	},
-}
-
-// mock information for getting context
-// factor out this into a context store package
-func current(ctx context.Context) context.Context {
-	// test backend address
-	return context.WithValue(ctx, backendAddressKey{}, "127.0.0.1:7654")
-}
-
-func connect(ctx context.Context) (*client.Client, error) {
-	address, err := BackendAddress(ctx)
-	if err != nil {
-		return nil, errors.Wrap(err, "no backend address")
-	}
-	c, err := client.New(address, 500*time.Millisecond)
-	if err != nil {
-		if err != context.DeadlineExceeded {
-			return nil, errors.Wrap(err, "connect to backend")
-		}
-		// the backend is not running so start it
-		cmd := exec.Command("backend-example", "--address", address)
-		go cmd.Wait()
-
-		if err := cmd.Start(); err != nil {
-			return nil, errors.Wrap(err, "start backend")
-		}
-		return client.New(address, 2*time.Second)
-	}
-	return c, nil
-}
-
-type backendAddressKey struct{}
-
-func BackendAddress(ctx context.Context) (string, error) {
-	v, ok := ctx.Value(backendAddressKey{}).(string)
-	if !ok {
-		return "", errors.New("no backend address key")
-	}
-	return v, nil
-}
+}

+ 107 - 0
cmd/example.go

@@ -0,0 +1,107 @@
+/*
+	Copyright (c) 2019 Docker Inc.
+
+	Permission is hereby granted, free of charge, to any person
+	obtaining a copy of this software and associated documentation
+	files (the "Software"), to deal in the Software without
+	restriction, including without limitation the rights to use, copy,
+	modify, merge, publish, distribute, sublicense, and/or sell copies
+	of the Software, and to permit persons to whom the Software is
+	furnished to do so, subject to the following conditions:
+
+	The above copyright notice and this permission notice shall be
+	included in all copies or substantial portions of the Software.
+
+	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+	EXPRESS OR IMPLIED,
+	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+	HOLDERS BE LIABLE FOR ANY CLAIM,
+	DAMAGES OR OTHER LIABILITY,
+	WHETHER IN AN ACTION OF CONTRACT,
+	TORT OR OTHERWISE,
+	ARISING FROM, OUT OF OR IN CONNECTION WITH
+	THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+package main
+
+import (
+	"context"
+	"encoding/json"
+	"os"
+	"os/exec"
+	"time"
+
+	"github.com/docker/api/client"
+	"github.com/gogo/protobuf/types"
+	"github.com/pkg/errors"
+	"github.com/urfave/cli"
+)
+
+var exampleCommand = cli.Command{
+	Name:  "example",
+	Usage: "sample command using backend, to be removed later",
+	Action: func(clix *cli.Context) error {
+		// return information for the current context
+		ctx, cancel := client.NewContext()
+		defer cancel()
+
+		// get our current context
+		ctx = current(ctx)
+
+		client, err := connect(ctx)
+		if err != nil {
+			return errors.Wrap(err, "cannot connect to backend")
+		}
+		defer client.Close()
+
+		info, err := client.BackendInformation(ctx, &types.Empty{})
+		if err != nil {
+			return errors.Wrap(err, "fetch backend information")
+		}
+		enc := json.NewEncoder(os.Stdout)
+		enc.SetIndent("", " ")
+		return enc.Encode(info)
+	},
+}
+
+// mock information for getting context
+// factor out this into a context store package
+func current(ctx context.Context) context.Context {
+	// test backend address
+	return context.WithValue(ctx, backendAddressKey{}, "127.0.0.1:7654")
+}
+
+func connect(ctx context.Context) (*client.Client, error) {
+	address, err := BackendAddress(ctx)
+	if err != nil {
+		return nil, errors.Wrap(err, "no backend address")
+	}
+	c, err := client.New(address, 500*time.Millisecond)
+	if err != nil {
+		if err != context.DeadlineExceeded {
+			return nil, errors.Wrap(err, "connect to backend")
+		}
+		// the backend is not running so start it
+		cmd := exec.Command("backend-example", "--address", address)
+		go cmd.Wait()
+
+		if err := cmd.Start(); err != nil {
+			return nil, errors.Wrap(err, "start backend")
+		}
+		return client.New(address, 2*time.Second)
+	}
+	return c, nil
+}
+
+type backendAddressKey struct{}
+
+func BackendAddress(ctx context.Context) (string, error) {
+	v, ok := ctx.Value(backendAddressKey{}).(string)
+	if !ok {
+		return "", errors.New("no backend address key")
+	}
+	return v, nil
+}

+ 12 - 0
cmd/main.go

@@ -30,11 +30,22 @@ package main
 import (
 import (
 	"fmt"
 	"fmt"
 	"os"
 	"os"
+	"path/filepath"
 
 
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 	"github.com/urfave/cli"
 	"github.com/urfave/cli"
 )
 )
 
 
+func init() {
+	// initial hack to get the path of the project's bin dir
+	// into the env of this cli for development
+
+	path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/docker/api/bin")
+	if err := os.Setenv("PATH", fmt.Sprintf("$PATH:%s", path)); err != nil {
+		panic(err)
+	}
+}
+
 func main() {
 func main() {
 	app := cli.NewApp()
 	app := cli.NewApp()
 	app.Name = "docker"
 	app.Name = "docker"
@@ -55,6 +66,7 @@ func main() {
 	}
 	}
 	app.Commands = []cli.Command{
 	app.Commands = []cli.Command{
 		contextCommand,
 		contextCommand,
+		exampleCommand,
 	}
 	}
 	if err := app.Run(os.Args); err != nil {
 	if err := app.Run(os.Args); err != nil {
 		fmt.Fprintln(os.Stderr, err)
 		fmt.Fprintln(os.Stderr, err)