Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions cmd/cli/subcommand/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package subcommand
import (
"fmt"
"log/slog"
"os"

"github.com/rokuosan/github-issue-cms/pkg/config"
"github.com/spf13/cobra"
Expand All @@ -14,6 +15,7 @@ func NewInitCommand() *cobra.Command {
var (
username string
repository string
force bool
)

cmd := &cobra.Command{
Expand All @@ -34,18 +36,28 @@ Examples:
# Short form
github-issue-cms init -u yourname -r yourrepo`,
RunE: func(cmd *cobra.Command, args []string) error {
return runInit(cmd, username, repository)
return runInit(cmd, username, repository, force)
},
}

// Define flags.
cmd.Flags().StringVarP(&username, "username", "u", "", "GitHub username")
cmd.Flags().StringVarP(&repository, "repository", "r", "", "GitHub repository name")
cmd.Flags().BoolVar(&force, "force", false, "Overwrite an existing configuration file")

return cmd
}

func runInit(cmd *cobra.Command, username, repository string) error {
func runInit(cmd *cobra.Command, username, repository string, force bool) error {
configPath := config.GetConfigPath()
if !force {
if _, err := os.Stat(configPath); err == nil {
return fmt.Errorf("configuration file already exists: %s (use --force to overwrite)", configPath)
} else if !os.IsNotExist(err) {
return fmt.Errorf("failed to inspect configuration file %s: %w", configPath, err)
}
}

slog.Info("Generating configuration file...")

// Generate the config file.
Expand Down Expand Up @@ -74,7 +86,6 @@ func runInit(cmd *cobra.Command, username, repository string) error {
return fmt.Errorf("failed to write config file: %w", err)
}

configPath := config.GetConfigPath()
slog.Info("Configuration file created: " + configPath)

// Show the resulting configuration values.
Expand Down
22 changes: 22 additions & 0 deletions cmd/cli/subcommand/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ func TestInitCommand_ShortFlags(t *testing.T) {
assert.Contains(t, content, "shortrepo")
}

func TestInitCommand_RefusesToOverwriteExistingConfig(t *testing.T) {
tempDir := t.TempDir()
originalWd, err := os.Getwd()
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, os.Chdir(originalWd)) })
require.NoError(t, os.Chdir(tempDir))

configPath := filepath.Join(tempDir, config.ConfigFileName+"."+config.ConfigFileType)
original := "github:\n username: existing\n"
require.NoError(t, os.WriteFile(configPath, []byte(original), 0o644))

cmd := NewInitCommand()
cmd.SetArgs([]string{})
err = cmd.Execute()

require.Error(t, err)
assert.Contains(t, err.Error(), "already exists")
data, readErr := os.ReadFile(configPath)
require.NoError(t, readErr)
assert.Equal(t, original, string(data))
}

func TestInitCommand_Examples(t *testing.T) {
cmd := NewInitCommand()

Expand Down
Loading