diff --git a/cmds/uptool/.gitignore b/cmds/uptool/.gitignore new file mode 100644 index 00000000..c5074cf6 --- /dev/null +++ b/cmds/uptool/.gitignore @@ -0,0 +1 @@ +uptool diff --git a/cmds/uptool/main.go b/cmds/uptool/main.go new file mode 100644 index 00000000..fe22d78d --- /dev/null +++ b/cmds/uptool/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "os" + "path/filepath" + + "github.com/safing/portbase/updater" + "github.com/safing/portbase/utils" + "github.com/spf13/cobra" +) + +var registry *updater.ResourceRegistry + +var rootCmd = &cobra.Command{ + Use: "uptool", + Short: "A simple tool to assist in the update and release process", + Args: cobra.ExactArgs(1), + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + absPath, err := filepath.Abs(args[0]) + if err != nil { + return err + } + + registry = &updater.ResourceRegistry{} + return registry.Initialize(utils.NewDirStructure(absPath, 0o755)) + }, + SilenceUsage: true, +} + +func main() { + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } +} diff --git a/cmds/uptool/scan.go b/cmds/uptool/scan.go new file mode 100644 index 00000000..8ec15905 --- /dev/null +++ b/cmds/uptool/scan.go @@ -0,0 +1,80 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "path/filepath" + "strings" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(scanCmd) +} + +var scanCmd = &cobra.Command{ + Use: "scan", + Short: "Scan the specified directory and print the result", + Args: cobra.ExactArgs(1), + RunE: scan, +} + +func scan(cmd *cobra.Command, args []string) error { + err := scanStorage() + if err != nil { + return err + } + + // export beta + data, err := json.MarshalIndent(exportSelected(true), "", " ") + if err != nil { + return err + } + // print + fmt.Println("beta:") + fmt.Println(string(data)) + + // export stable + data, err = json.MarshalIndent(exportSelected(false), "", " ") + if err != nil { + return err + } + // print + fmt.Println("\nstable:") + fmt.Println(string(data)) + + return nil +} + +func scanStorage() error { + files, err := ioutil.ReadDir(registry.StorageDir().Path) + if err != nil { + return err + } + + // scan "all" and all "os_platform" dirs + for _, file := range files { + if file.IsDir() && (file.Name() == "all" || strings.Contains(file.Name(), "_")) { + err := registry.ScanStorage(filepath.Join(registry.StorageDir().Path, file.Name())) + if err != nil { + return err + } + } + } + + return nil +} + +func exportSelected(beta bool) map[string]string { + registry.SetBeta(beta) + registry.SelectVersions() + export := registry.Export() + + versions := make(map[string]string) + for _, rv := range export { + versions[rv.Identifier] = rv.SelectedVersion.VersionNumber + } + return versions +} diff --git a/cmds/uptool/update.go b/cmds/uptool/update.go new file mode 100644 index 00000000..442c31eb --- /dev/null +++ b/cmds/uptool/update.go @@ -0,0 +1,64 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "path/filepath" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(updateCmd) +} + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update scans the specified directory and registry the index and symlink structure", + Args: cobra.ExactArgs(1), + RunE: update, +} + +func update(cmd *cobra.Command, args []string) error { + err := scanStorage() + if err != nil { + return err + } + + // export beta + data, err := json.MarshalIndent(exportSelected(true), "", " ") + if err != nil { + return err + } + // print + fmt.Println("beta:") + fmt.Println(string(data)) + // write index + err = ioutil.WriteFile(filepath.Join(registry.StorageDir().Dir, "beta.json"), data, 0o644) //nolint:gosec // 0644 is intended + if err != nil { + return err + } + + // export stable + data, err = json.MarshalIndent(exportSelected(false), "", " ") + if err != nil { + return err + } + // print + fmt.Println("\nstable:") + fmt.Println(string(data)) + // write index + err = ioutil.WriteFile(filepath.Join(registry.StorageDir().Dir, "stable.json"), data, 0o644) //nolint:gosec // 0644 is intended + if err != nil { + return err + } + // create symlinks + err = registry.CreateSymlinks(registry.StorageDir().ChildDir("latest", 0o755)) + if err != nil { + return err + } + fmt.Println("\nstable symlinks created") + + return nil +}