Skip to content

Commit

Permalink
Merge branch 'main' into import-yaml-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Sep 26, 2023
2 parents 0177869 + 654968e commit 4d031a6
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 23 deletions.
20 changes: 20 additions & 0 deletions build/testing/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ exit $?`,
}
}

{
container := container.Pipeline("flipt config init")

container, err := assertExec(ctx, container, flipt("config", "init", "-y"))
if err != nil {
return err
}

contents, err := container.File("/home/flipt/.config/flipt/config.yml").Contents(ctx)
if err != nil {
return err
}

expected := `# yaml-language-server: $schema=https://raw.githubusercontent.com/flipt-io/flipt/main/config/flipt.schema.json`

if !strings.Contains(contents, expected) {
return fmt.Errorf("unexpected output: %q does not contain %q", contents, expected)
}
}

return nil
}

Expand Down
116 changes: 93 additions & 23 deletions cmd/flipt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"bytes"
"fmt"
"os"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
Expand All @@ -15,44 +17,54 @@ type initCommand struct {
}

func (c *initCommand) run(cmd *cobra.Command, args []string) error {
var (
file string
defaultFile = providedConfigFile
)
defaultFile := providedConfigFile

if defaultFile == "" {
defaultFile = userConfigFile
}

q := []*survey.Question{
{
Name: "file",
Prompt: &survey.Input{
Message: "Configuration file path:",
Default: defaultFile,
file := defaultFile

ack := c.force
if !ack {
q := []*survey.Question{
{
Name: "file",
Prompt: &survey.Input{
Message: "Configuration file path:",
Default: defaultFile,
},
Validate: survey.Required,
},
Validate: survey.Required,
},
}
}

if err := survey.Ask(q, &file); err != nil {
return err
}
if err := survey.Ask(q, &file); err != nil {
return err
}

overwrite := c.force
if !overwrite {
// check if file exists
if _, err := os.Stat(file); err == nil {
// file exists
prompt := &survey.Confirm{
Message: "File exists. Overwrite?",
}
if err := survey.AskOne(prompt, &overwrite); err != nil {
if err := survey.AskOne(prompt, &ack); err != nil {
return err
}
} else if !os.IsNotExist(err) {
return err
}
}

if !ack {
return nil
}

// get path to file, create directory if not exists
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
return err
}

cfg := config.Default()
cfg.Version = config.Version // write version for backward compatibility
out, err := yaml.Marshal(cfg)
Expand All @@ -64,16 +76,67 @@ func (c *initCommand) run(cmd *cobra.Command, args []string) error {
b.WriteString("# yaml-language-server: $schema=https://raw.githubusercontent.com/flipt-io/flipt/main/config/flipt.schema.json\n\n")
b.Write(out)

return os.WriteFile(file, b.Bytes(), 0600)
if err := os.WriteFile(file, b.Bytes(), 0600); err != nil {
return err
}

fmt.Printf("Configuration file written to %s\n", file)
return nil
}

type editCommand struct{}

func (c *editCommand) run(cmd *cobra.Command, args []string) error {
// TODO: check if no TTY
file := providedConfigFile

if file == "" {
file = userConfigFile
}

b, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("reading config file: %w", err)
}

content := string(b)

if err := survey.AskOne(&survey.Editor{
Message: "Edit configuration",
Default: string(b),
HideDefault: true,
AppendDefault: true,
FileName: "flipt*.yml",
}, &content); err != nil {
return err
}

// create if not exists, truncate if exists
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("creating config file: %w", err)
}

defer f.Close()

if _, err := f.WriteString(content); err != nil {
return fmt.Errorf("writing config file: %w", err)
}

return nil
}

func newConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage Flipt configuration",
Use: "config",
Short: "Manage Flipt configuration",
Aliases: []string{"cfg"},
}

initCmd := &initCommand{}
var (
initCmd = &initCommand{}
editCmd = &editCommand{}
)

var init = &cobra.Command{
Use: "init",
Expand All @@ -83,8 +146,15 @@ func newConfigCommand() *cobra.Command {

init.Flags().BoolVarP(&initCmd.force, "force", "y", false, "Overwrite existing configuration file")

var edit = &cobra.Command{
Use: "edit",
Short: "Edit Flipt configuration",
RunE: editCmd.run,
}

cmd.PersistentFlags().StringVar(&providedConfigFile, "config", "", "path to config file")
cmd.AddCommand(init)
cmd.AddCommand(edit)

return cmd
}

0 comments on commit 4d031a6

Please sign in to comment.