1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/spf13/cobra"
)
var createCmd = &cobra.Command{
Use: "create <config name>",
Short: "Create a config",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New(
"create command takes a unique config name as an argument, and accepts config data via stdin or via a file")
}
flags := cmd.Flags()
var (
configData []byte
err error
)
if flags.Changed("file") {
filename, err := flags.GetString("file")
if err != nil {
return err
}
configData, err = ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("Error reading from file '%s': %s", filename, err.Error())
}
} else {
configData, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Error reading content from STDIN: %s", err.Error())
}
}
client, err := common.Dial(cmd)
if err != nil {
return err
}
spec := &api.ConfigSpec{
Annotations: api.Annotations{Name: args[0]},
Data: configData,
}
resp, err := client.CreateConfig(common.Context(cmd), &api.CreateConfigRequest{Spec: spec})
if err != nil {
return err
}
fmt.Println(resp.Config.ID)
return nil
},
}
func init() {
createCmd.Flags().StringP("file", "f", "", "Rather than read the config from STDIN, read from the given file")
}
|