-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
95 lines (81 loc) · 3.25 KB
/
update.go
File metadata and controls
95 lines (81 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/stacktodate/stacktodate-cli/cmd/helpers"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
// updateCmd updates an existing stacktodate.yml file's stack using autodetect
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update stack in a stacktodate.yml using autodetect",
Long: "Run autodetect and update the provided stacktodate.yml's stack, preserving uuid and name.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Load existing config without requiring UUID
config, err := helpers.LoadConfig(updateConfigFile)
if err != nil {
helpers.ExitOnError(err, "failed to load config")
}
// Resolve absolute path
absTargetFile, err := helpers.ResolveAbsPath(updateConfigFile)
if err != nil {
if updateConfigFile == "" {
absTargetFile, _ = helpers.ResolveAbsPath("stacktodate.yml")
} else {
helpers.ExitOnError(err, "failed to resolve config path")
}
}
// Get target directory
targetDir, err := helpers.GetConfigDir(absTargetFile)
if err != nil {
helpers.ExitOnError(err, "failed to get config directory")
}
fmt.Printf("Updating stack in: %s\n", updateConfigFile)
// Detect project information in target directory
reader := bufio.NewReader(os.Stdin)
var detectedTechs map[string]helpers.StackEntry
if !skipAutodetect {
err = helpers.WithWorkingDir(targetDir, func() error {
info := DetectProjectInfo()
PrintDetectedInfo(info)
detectedTechs = selectCandidates(reader, info)
return nil
})
if err != nil {
helpers.ExitOnError(err, "failed to detect project")
}
} else {
// If autodetect is skipped, keep existing stack
detectedTechs = config.Stack
}
// Preserve UUID and Name, update Stack
config.Stack = detectedTechs
// Marshal to YAML
data, err := yaml.Marshal(&config)
if err != nil {
helpers.ExitOnError(err, "failed to create configuration")
}
// Write back to the original absolute file path
if err := os.WriteFile(absTargetFile, data, 0644); err != nil {
helpers.ExitOnError(err, "failed to write config")
}
fmt.Println("\nStack updated successfully!")
if len(detectedTechs) > 0 {
fmt.Println("Updated stack:")
for tech, entry := range detectedTechs {
fmt.Printf(" %s: %s (from: %s)\n", tech, entry.Version, entry.Source)
}
}
},
}
var updateConfigFile string
func init() {
rootCmd.AddCommand(updateCmd)
// Flags for update command
updateCmd.Flags().StringVarP(&updateConfigFile, "config", "c", "stacktodate.yml", "Path to stacktodate.yml config file (default: stacktodate.yml)")
updateCmd.Flags().BoolVar(&skipAutodetect, "skip-autodetect", false, "Skip autodetection of project technologies")
updateCmd.Flags().BoolVar(&noInteractive, "no-interactive", false, "Use first candidate by default without prompting")
}