Skip to content

improve init command to create v2/v1 yaml file #2335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2023
Merged
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
29 changes: 27 additions & 2 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (

func init() {
uploadCmd.Flags().BoolP("dry-run", "", false, "dump upload request (default: false)")
initCmd.Flags().BoolP("v1", "", false, "generate v1 config yaml file")
initCmd.Flags().BoolP("v2", "", true, "generate v2 config yaml file")
initCmd.MarkFlagsMutuallyExclusive("v1", "v2")
}

// Do runs the command logic.
Expand Down Expand Up @@ -88,6 +91,17 @@ var initCmd = &cobra.Command{
Use: "init",
Short: "Create an empty sqlc.yaml settings file",
RunE: func(cmd *cobra.Command, args []string) error {
useV1, err := cmd.Flags().GetBool("v1")
if err != nil {
return err
}
var yamlConfig interface{}
if useV1 {
yamlConfig = config.V1GenerateSettings{Version: "1"}
} else {
yamlConfig = config.Config{Version: "2"}
}

defer trace.StartRegion(cmd.Context(), "init").End()
file := "sqlc.yaml"
if f := cmd.Flag("file"); f != nil && f.Changed {
Expand All @@ -97,13 +111,24 @@ var initCmd = &cobra.Command{
}
}
if _, err := os.Stat(file); !os.IsNotExist(err) {
fmt.Printf("%s is already created\n", file)
return nil
}
blob, err := yaml.Marshal(config.V1GenerateSettings{Version: "1"})
blob, err := yaml.Marshal(yamlConfig)
if err != nil {
return err
}
return os.WriteFile(file, blob, 0644)
err = os.WriteFile(file, blob, 0644)
if err != nil {
return err
}
configDoc := "https://docs.sqlc.dev/en/stable/reference/config.html"
fmt.Printf(
"%s is added. Please visit %s to learn more about configuration\n",
file,
configDoc,
)
return nil
},
}

Expand Down