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