Skip to content

Commit d05d8e9

Browse files
committed
feat: pre-fill config name when updating existing config
When choosing "Update existing config" during snapshot upload, the config name input is now pre-filled with the existing slug converted to title case instead of requiring the user to retype it.
1 parent 53bba66 commit d05d8e9

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

internal/cli/push.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func pushSnapshot(data []byte, slug, token, username, apiBase string) error {
8989
return fmt.Errorf("parse snapshot: %w", err)
9090
}
9191

92-
name, desc, visibility, err := promptPushDetails()
92+
name, desc, visibility, err := promptPushDetails("")
9393
if err != nil {
9494
return err
9595
}
@@ -122,7 +122,7 @@ func pushConfig(data []byte, slug, token, username, apiBase string) error {
122122
return fmt.Errorf("invalid config: %w", err)
123123
}
124124

125-
name, desc, visibility, err := promptPushDetails()
125+
name, desc, visibility, err := promptPushDetails("")
126126
if err != nil {
127127
return err
128128
}
@@ -256,9 +256,15 @@ func doUpload(url string, body []byte, token, username, slug string) error {
256256
return nil
257257
}
258258

259-
func promptPushDetails() (string, string, string, error) {
259+
func promptPushDetails(defaultName string) (string, string, string, error) {
260260
fmt.Fprintln(os.Stderr)
261-
name, err := ui.Input("Config name", "My Mac Setup")
261+
var name string
262+
var err error
263+
if defaultName != "" {
264+
name, err = ui.InputWithDefault("Config name", "My Mac Setup", defaultName)
265+
} else {
266+
name, err = ui.Input("Config name", "My Mac Setup")
267+
}
262268
if err != nil {
263269
return "", "", "", fmt.Errorf("get config name: %w", err)
264270
}

internal/cli/snapshot.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ func uploadSnapshot(snap *snapshot.Snapshot) error {
230230
return err
231231
}
232232

233-
configName, configDesc, visibility, err := promptPushDetails()
233+
defaultName := ""
234+
if updateSlug != "" {
235+
defaultName = slugToTitle(updateSlug)
236+
}
237+
238+
configName, configDesc, visibility, err := promptPushDetails(defaultName)
234239
if err != nil {
235240
return err
236241
}
@@ -256,6 +261,17 @@ func uploadSnapshot(snap *snapshot.Snapshot) error {
256261
return nil
257262
}
258263

264+
// slugToTitle converts a URL slug like "my-mac-setup" to "My Mac Setup".
265+
func slugToTitle(slug string) string {
266+
words := strings.Split(slug, "-")
267+
for i, w := range words {
268+
if len(w) > 0 {
269+
words[i] = strings.ToUpper(w[:1]) + w[1:]
270+
}
271+
}
272+
return strings.Join(words, " ")
273+
}
274+
259275
func promptUpdateOrCreate() (string, error) {
260276
source, err := syncpkg.LoadSource()
261277
if err != nil || source == nil || source.Username == "" || source.Slug == "" {

internal/ui/ui.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ func SelectOption(title string, options []string) (string, error) {
212212
}
213213

214214
func Input(title, placeholder string) (string, error) {
215-
var value string
215+
return InputWithDefault(title, placeholder, "")
216+
}
217+
218+
func InputWithDefault(title, placeholder, defaultValue string) (string, error) {
219+
value := defaultValue
216220

217221
form := huh.NewForm(
218222
huh.NewGroup(

0 commit comments

Comments
 (0)