Skip to content

Commit

Permalink
Merge pull request #94 from hazelops/TOOLS-202-ize-secrets-edit-app
Browse files Browse the repository at this point in the history
TOOLS-202 add ize secrets edit command
  • Loading branch information
psihachina authored Jan 24, 2022
2 parents 4ed1e53 + 2862152 commit 6ff4215
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 4 deletions.
3 changes: 1 addition & 2 deletions internal/commands/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"github.com/spf13/cobra"
)


func NewCmdSecrets() *cobra.Command {
cmd := &cobra.Command{
Use: "secrets",
Short: "manage secrets",
TraverseChildren: true,

}

cmd.AddCommand(
NewCmdSecretsRemove(),
NewCmdSecretsPush(),
NewCmdSecretsEdit(),
)

return cmd
Expand Down
137 changes: 137 additions & 0 deletions internal/commands/secrets/secrets_edit.go
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
}
4 changes: 2 additions & 2 deletions internal/commands/secrets/secrets_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type SecretsPushOptions struct {
Force bool
}

func NewSecretsSetFlags() *SecretsPushOptions {
func NewSecretsPushFlags() *SecretsPushOptions {
return &SecretsPushOptions{}
}

func NewCmdSecretsPush() *cobra.Command {
o := NewSecretsSetFlags()
o := NewSecretsPushFlags()

cmd := &cobra.Command{
Use: "push",
Expand Down

0 comments on commit 6ff4215

Please sign in to comment.