diff --git a/cmds/updatemgr/main.go b/cmds/updatemgr/main.go index 1d998581..e7703d77 100644 --- a/cmds/updatemgr/main.go +++ b/cmds/updatemgr/main.go @@ -1,71 +1,20 @@ package main import ( - "encoding/json" - "flag" "fmt" "os" - "github.com/safing/portmaster/service/updates" + "github.com/spf13/cobra" ) -var bundleSettings = updates.BundleFileSettings{ - Name: "Portmaster Binaries", - PrimaryArtifact: "linux_amd64/portmaster-core", - BaseURL: "https://updates.safing.io/", - IgnoreFiles: []string{ - // Indexes, checksums, latest symlinks. - "*.json", - "sha256*.txt", - "latest/**", - - // Signatures. - "*.sig", - "**/*.sig", - - // Related, but not required artifacts. - "**/*.apk", - "**/*install*", - "**/spn-hub*", - "**/jess*", - "**/hubs*.json", - "**/*mini*.mmdb.gz", - - // Deprecated artifacts. - "**/profilemgr*.zip", - "**/settings*.zip", - "**/monitor*.zip", - "**/base*.zip", - "**/console*.zip", - "**/portmaster-wintoast*.dll", - "**/portmaster-snoretoast*.exe", - "**/portmaster-kext*.dll", - }, - UnpackFiles: map[string]string{ - "gz": "**/*.gz", - "zip": "**/app2/**/portmaster-app*.zip", - }, +var rootCmd = &cobra.Command{ + Use: "updatemgr", + Short: "Manage update artifacts.", } func main() { - dir := flag.String("dir", "", "path to the directory that contains the artifacts") - - flag.Parse() - if *dir == "" { - fmt.Fprintf(os.Stderr, "-dir parameter is required\n") - return + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) } - - bundle, err := updates.GenerateBundleFromDir(*dir, bundleSettings) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to generate bundle: %s\n", err) - return - } - - bundleStr, err := json.MarshalIndent(&bundle, "", " ") - if err != nil { - fmt.Fprintf(os.Stderr, "failed to marshal bundle: %s\n", err) - } - - fmt.Printf("%s", bundleStr) } diff --git a/cmds/updatemgr/scan.go b/cmds/updatemgr/scan.go new file mode 100644 index 00000000..fde03eff --- /dev/null +++ b/cmds/updatemgr/scan.go @@ -0,0 +1,78 @@ +package main + +import ( + "encoding/json" + "fmt" + + "github.com/safing/portmaster/service/updates" + "github.com/spf13/cobra" +) + +var ( + bundleSettings = updates.BundleFileSettings{ + Name: "Portmaster Binaries", + PrimaryArtifact: "linux_amd64/portmaster-core", + BaseURL: "https://updates.safing.io/", + IgnoreFiles: []string{ + // Indexes, checksums, latest symlinks. + "*.json", + "sha256*.txt", + "latest/**", + + // Signatures. + "*.sig", + "**/*.sig", + + // Related, but not required artifacts. + "**/*.apk", + "**/*install*", + "**/spn-hub*", + "**/jess*", + "**/hubs*.json", + "**/*mini*.mmdb.gz", + + // Deprecated artifacts. + "**/profilemgr*.zip", + "**/settings*.zip", + "**/monitor*.zip", + "**/base*.zip", + "**/console*.zip", + "**/portmaster-wintoast*.dll", + "**/portmaster-snoretoast*.exe", + "**/portmaster-kext*.dll", + }, + UnpackFiles: map[string]string{ + "gz": "**/*.gz", + "zip": "**/app2/**/portmaster-app*.zip", + }, + } + + scanCmd = &cobra.Command{ + Use: "scan", + Short: "Scans the contents of the specified directory and creates an index from it.", + RunE: scan, + } + + bundleDir string +) + +func init() { + rootCmd.AddCommand(scanCmd) + scanCmd.Flags().StringVarP(&bundleDir, "dir", "d", "", "directory to create index from (required)") + _ = scanCmd.MarkFlagRequired("dir") +} + +func scan(cmd *cobra.Command, args []string) error { + bundle, err := updates.GenerateBundleFromDir(bundleDir, bundleSettings) + if err != nil { + return err + } + + bundleStr, err := json.MarshalIndent(&bundle, "", " ") + if err != nil { + return fmt.Errorf("marshal index: %w", err) + } + + fmt.Printf("%s", bundleStr) + return nil +}