@@ -2,49 +2,72 @@ package main
22
33import (
44 "bytes"
5- "flag"
65 "fmt"
76 "io"
8- "log"
97 "net/http"
108 "os"
119 "os/exec"
1210 "path/filepath"
1311 "runtime"
1412 "time"
1513
14+ "github.com/spf13/cobra"
1615 "gopkg.in/yaml.v3"
1716 "gradient-engineer/playbook"
1817)
1918
19+ var (
20+ playbookPath string
21+ outDir string
22+ )
23+
2024func main () {
21- var playbookPath string
22- var outDir string
25+ var rootCmd = & cobra.Command {
26+ Use : "toolbox-generator [flags]" ,
27+ Short : "Generate toolbox archives from playbook configurations" ,
28+ Long : `Toolbox Generator creates portable toolbox archives containing Nix packages
29+ and diagnostic tools defined in playbook configurations. The generated archives
30+ include all necessary dependencies and can be distributed and executed on
31+ target systems.` ,
32+ PreRunE : func (cmd * cobra.Command , args []string ) error {
33+ if playbookPath == "" {
34+ return fmt .Errorf ("playbook path is required" )
35+ }
36+ if runtime .GOOS != "linux" {
37+ return fmt .Errorf ("this utility must run on Linux" )
38+ }
39+ return nil
40+ },
41+ RunE : func (cmd * cobra.Command , args []string ) error {
42+ return generateToolbox ()
43+ },
44+ }
2345
24- flag .StringVar (& playbookPath , "playbook" , "" , "Path to playbook file" )
25- flag .StringVar (& outDir , "out" , "." , "Output directory" )
26- flag .Parse ()
46+ // Define flags
47+ rootCmd .Flags ().StringVarP (& playbookPath , "playbook" , "p" , "" , "Path to playbook file (required)" )
48+ rootCmd .Flags ().StringVarP (& outDir , "out" , "o" , "." , "Output directory for generated archive" )
49+
50+ // Mark required flags
51+ rootCmd .MarkFlagRequired ("playbook" )
2752
28- if playbookPath == "" {
29- fmt .Fprintln (os .Stderr , "error: -playbook path is required" )
30- os .Exit (2 )
31- }
32- if runtime .GOOS != "linux" {
33- fmt .Fprintln (os .Stderr , "error: this utility must run on Linux" )
34- os .Exit (2 )
53+ // Execute the command
54+ if err := rootCmd .Execute (); err != nil {
55+ os .Exit (1 )
3556 }
57+ }
3658
59+ func generateToolbox () error {
3760 cfg , err := readPlaybook (playbookPath )
3861 if err != nil {
39- log . Fatalf ("failed to read playbook: %v " , err )
62+ return fmt . Errorf ("failed to read playbook: %w " , err )
4063 }
4164 if len (cfg .Nixpkgs .Packages ) == 0 {
42- log . Fatalf ("no nixpkgs.packages listed in %s" , playbookPath )
65+ return fmt . Errorf ("no nixpkgs.packages listed in %s" , playbookPath )
4366 }
4467
4568 workDir , err := os .MkdirTemp ("" , "toolbox_work_*" )
4669 if err != nil {
47- log . Fatalf ("failed to create temporary workdir: %v " , err )
70+ return fmt . Errorf ("failed to create temporary workdir: %w " , err )
4871 }
4972 defer func () {
5073 _ = exec .Command ("chmod" , "-R" , "u+w" , workDir ).Run ()
@@ -53,29 +76,30 @@ func main() {
5376
5477 toolboxDir , _ := filepath .Abs (filepath .Join (workDir , "toolbox" ))
5578 if err := nixCopy (toolboxDir , cfg .Nixpkgs .Version , cfg .Nixpkgs .Packages ); err != nil {
56- log . Fatalf ("nix copy failed: %v " , err )
79+ return fmt . Errorf ("nix copy failed: %w " , err )
5780 }
5881
5982 if err := fetchAndInstallProot (toolboxDir ); err != nil {
60- log . Fatalf ("failed to install proot: %v " , err )
83+ return fmt . Errorf ("failed to install proot: %w " , err )
6184 }
6285
6386 // Include the playbook file inside the toolbox directory
6487 if err := copyFile (playbookPath , filepath .Join (toolboxDir , "playbook.yaml" ), 0o644 ); err != nil {
65- log . Fatalf ("failed to copy playbook file: %v " , err )
88+ return fmt . Errorf ("failed to copy playbook file: %w " , err )
6689 }
6790
6891 outDir , _ = filepath .Abs (outDir )
6992 if err := os .MkdirAll (outDir , 0o755 ); err != nil {
70- log . Fatalf ("failed to ensure output directory: %v " , err )
93+ return fmt . Errorf ("failed to ensure output directory: %w " , err )
7194 }
7295 archiveName := fmt .Sprintf ("%s.%s.%s.tar.xz" , cfg .ID , runtime .GOOS , runtime .GOARCH )
7396 outPath := filepath .Join (outDir , archiveName )
7497 if err := createTarXz (outPath , toolboxDir ); err != nil {
75- log . Fatalf ("failed to create tar.xz: %v " , err )
98+ return fmt . Errorf ("failed to create tar.xz: %w " , err )
7699 }
77100
78101 fmt .Printf ("created %s\n " , outPath )
102+ return nil
79103}
80104
81105func readPlaybook (path string ) (* playbook.PlaybookConfig , error ) {
0 commit comments