Skip to content

Commit

Permalink
Update yaml format
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoConstantine committed Jul 2, 2024
1 parent 803a7e6 commit fe9a833
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
8 changes: 5 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
tools:
- neovim
casks:
- alacritty
- name: "neovim" # Will use Homebrew by default
- name: "alacritty"
method: "cask" # Specifies this is a Homebrew cask
- name: "uv"
install_command: "curl -LsSf https://astral.sh/uv/install.sh | sh"
47 changes: 28 additions & 19 deletions pkg/commands/install/homebrew/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func NewInstallCmd(iostream *iostreams.IOStreams) *cobra.Command {
cs := iostream.ColorScheme()
var configFile string
var force bool

cmd := &cobra.Command{
Use: "tools",
Expand All @@ -32,38 +33,46 @@ func NewInstallCmd(iostream *iostreams.IOStreams) *cobra.Command {
fmt.Fprintf(iostream.ErrOut, cs.Red("Error loading configuration: %v\n"), err)
return err
}
return InstallToolsFromConfig(iostream, config, ctx)
return InstallToolsFromConfig(iostream, config, ctx, force)
},
}
cmd.Flags().StringVarP(&configFile, "config", "c", "config.yaml", "Path to the configuration file")
cmd.Flags().BoolVarP(&force, "force", "f", false, "Force reinstall of casks")
return cmd
}

func InstallToolsFromConfig(iostream *iostreams.IOStreams, config *utils.ToolsConfig, ctx context.Context) error {
func InstallToolsFromConfig(iostream *iostreams.IOStreams, config *utils.ToolConfig, ctx context.Context, force bool) error {
cs := iostream.ColorScheme()
for _, tool := range config.Tools {
fmt.Printf("Installing tool %s...\n", tool)
if err := runBrewInstall(iostream, tool, false, ctx); err != nil {
return err
}
}
for _, cask := range config.Casks {
fmt.Printf("Installing cask %s...\n", cask)
if err := runBrewInstall(iostream, cask, true, ctx); err != nil {
return err
fmt.Fprintf(iostream.Out, cs.Green("Installing tool %s...\n"), tool)
if tool.InstallCommand != "" {
fmt.Fprintf(iostream.Out, "Installing %s using custom command...\n", tool.Name)
if err := executeCommand(tool.InstallCommand, ctx); err != nil {
fmt.Fprintf(iostream.ErrOut, cs.Red("Failed to install %s: %v\n"), tool.Name, err)
return err
}
} else {
// Default to Homebrew installation
command := "brew install"
if tool.Method == "cask" {
command += " --cask"
}
if force {
command += " --force"
}
fmt.Printf("Installing %s using Homebrew with %s...\n", tool.Name, command)
if err := executeCommand(fmt.Sprintf("%s %s", command, tool.Name), ctx); err != nil {
fmt.Fprintf(iostream.ErrOut, cs.Red("Failed to install %s: %v\n"), tool.Name, err)
return err
}
}
}
fmt.Println("All requested tools and casks have been installed successfully.")
return nil
}

// runBrewInstall runs the brew install command for a given tool or cask.
func runBrewInstall(iostream *iostreams.IOStreams, name string, isCask bool, ctx context.Context) error {
var cmd *exec.Cmd
if isCask {
cmd = exec.CommandContext(ctx, "brew", "install", "--cask", name)
} else {
cmd = exec.CommandContext(ctx, "brew", "install", name)
}
func executeCommand(command string, ctx context.Context) error {
cmd := exec.CommandContext(ctx, "sh", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
Expand Down
15 changes: 10 additions & 5 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ func IsAdmin(u *user.User) bool {
return strings.Contains(string(output), "admin")
}

type ToolsConfig struct {
Tools []string `yaml:"tools"`
Casks []string `yaml:"casks"`
type ToolConfig struct {
Tools []Tool `yaml:"tools"`
}

type Tool struct {
Name string `yaml:"name"`
Method string `yaml:"method,omitempty"` // Optional, for specifying 'cask' or other Homebrew methods
InstallCommand string `yaml:"install_command,omitempty"`
}

// LoadToolsConfig loads tool configuration from a YAML file.
func LoadToolsConfig(filename string) (*ToolsConfig, error) {
func LoadToolsConfig(filename string) (*ToolConfig, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var config ToolsConfig
var config ToolConfig
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, err
}
Expand Down

0 comments on commit fe9a833

Please sign in to comment.