Skip to content

Commit

Permalink
Merge pull request #14 from ikura-hamu/feat/init
Browse files Browse the repository at this point in the history
✨ initコマンド
  • Loading branch information
ikura-hamu committed Aug 23, 2024
2 parents 332a6da + ffc7bad commit 93c8adc
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
// "--fast"
]
}
108 changes: 108 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"cmp"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize the configuration file",
Long: `This command initializes the configuration file of q-cli interactively.`,
Run: func(cmd *cobra.Command, args []string) {
if initForce {
fmt.Printf("Overwriting the existing configuration file: %s\n", cmp.Or(viper.ConfigFileUsed(), "(config file not found)"))
} else {
if viper.ConfigFileUsed() != "" {
fmt.Println("Configuration file already exists.")
fmt.Println(viper.ConfigFileUsed())
return
}
}

oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
cobra.CheckErr(err)
defer func() {
err := term.Restore(int(os.Stdin.Fd()), oldState)
cobra.CheckErr(err)
}()

prompts := []struct {
prompt string
defaultValue string // if not set, the input value is required
configKey string
isPassword bool
}{
{"Enter the webhook host", "https://q.trap.jp", configKeyWebhookHost, false},
{"Enter the webhook ID", "", configKeyWebhookID, false},
{"Enter the webhook secret", "", configKeyWebhookSecret, true},
}

t := term.NewTerminal(os.Stdin, "")
for _, p := range prompts {
prompt := p.prompt
if p.defaultValue != "" {
prompt += fmt.Sprintf(" (default: %s)", p.defaultValue)
}
prompt = fmt.Sprintf("%s: ", prompt)

var input string
var err error
if p.isPassword {
input, err = t.ReadPassword(prompt)
} else {
t.SetPrompt(prompt)
input, err = t.ReadLine()
}
if err != nil {
cobra.CheckErr(err)
}

input = strings.TrimSpace(input)
if input == "" {
if p.defaultValue == "" {
cobra.CheckErr(fmt.Sprintf("%s is required", p.configKey))
}
input = p.defaultValue
}

viper.Set(p.configKey, input)
}

if initForce {
err = viper.WriteConfig()
} else {
err = viper.SafeWriteConfig()
}
cobra.CheckErr(err)
},
}

var (
initForce bool
)

func init() {
rootCmd.AddCommand(initCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// initCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
initCmd.Flags().BoolVarP(&initForce, "force", "f", false, "If provided, overwrite the existing configuration file")
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
golang.org/x/term v0.23.0
)

require (
Expand All @@ -29,7 +30,7 @@ require (
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
Expand Down

0 comments on commit 93c8adc

Please sign in to comment.