Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions cmd/swarmctl/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@ var createCmd = &cobra.Command{
Short: "Create a secret",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("create command takes a unique secret name as an argument, and accepts secret data via stdin")
return errors.New(
"create command takes a unique secret name as an argument, and accepts secret data via stdin or via a file")
}

secretData, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Error reading content from STDIN: %v", err)
flags := cmd.Flags()
var (
secretData []byte
err error
)

if flags.Changed("file") {
filename, err := flags.GetString("file")
if err != nil {
return err
}
secretData, err = ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("Error reading from file '%s': %s", filename, err.Error())
}
} else {
secretData, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Error reading content from STDIN: %s", err.Error())
}
}

client, err := common.Dial(cmd)
Expand All @@ -42,3 +60,7 @@ var createCmd = &cobra.Command{
return nil
},
}

func init() {
createCmd.Flags().StringP("file", "f", "", "Rather than read the secret from STDIN, read from the given file")
}