[WIP] Use cobra for updatemgr

This commit is contained in:
Daniel
2024-10-10 08:58:02 +02:00
parent 5d9088f27e
commit a1a4bf6325
2 changed files with 85 additions and 58 deletions

View File

@@ -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)
}

78
cmds/updatemgr/scan.go Normal file
View File

@@ -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
}