-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TOOLS-202 add ize secrets edit command
- Loading branch information
1 parent
0aa20aa
commit 2862152
Showing
3 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package secrets | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/hazelops/ize/internal/config" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type SecretsEditOptions struct { | ||
Config *config.Config | ||
AppName string | ||
FilePath string | ||
} | ||
|
||
func NewSecretsEditFlags() *SecretsEditOptions { | ||
return &SecretsEditOptions{} | ||
} | ||
|
||
func NewCmdSecretsEdit() *cobra.Command { | ||
o := NewSecretsEditFlags() | ||
|
||
cmd := &cobra.Command{ | ||
Use: "edit", | ||
Short: "edit secrets file", | ||
Long: "This open secrets file in default editor.", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
err := o.Complete(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = o.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = o.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&o.FilePath, "file", "", "file with secrets") | ||
|
||
return cmd | ||
} | ||
|
||
func (o *SecretsEditOptions) Complete(cmd *cobra.Command, args []string) error { | ||
cfg, err := config.InitializeConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.Config = cfg | ||
o.AppName = cmd.Flags().Args()[0] | ||
|
||
if o.FilePath == "" { | ||
o.FilePath = fmt.Sprintf("%s/%s/%s.json", viper.GetString("ENV_DIR"), "secrets", o.AppName) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *SecretsEditOptions) Validate() error { | ||
if len(o.Config.Env) == 0 { | ||
return fmt.Errorf("env must be specified") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *SecretsEditOptions) Run() error { | ||
absPath, err := filepath.Abs(o.FilePath) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
_, err = os.Stat(absPath) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
f, err := os.OpenFile(absPath, os.O_RDWR, os.ModeAppend) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
b, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
text := string(b) | ||
|
||
err = survey.AskOne( | ||
&survey.Editor{ | ||
Message: fmt.Sprintf("Edit %s secrets file", o.AppName), | ||
Default: text, | ||
AppendDefault: true, | ||
}, | ||
&text, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
err = f.Truncate(0) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
_, err = f.Seek(0, 0) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
_, err = io.WriteString(f, text) | ||
if err != nil { | ||
return fmt.Errorf("can't secrets edit: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters