From ca9834effa6504818afce4fd1e4006aa1a129a5a Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Aug 2019 16:46:20 +0200 Subject: [PATCH] Add symlink creation to updates module and uptool --- updates/latest.go | 37 +++++++++++++++++++++++++++++++++ updates/uptool/root.go | 19 +++++++++++++++-- updates/uptool/update.go | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 updates/uptool/update.go diff --git a/updates/latest.go b/updates/latest.go index 91251359..0e96af8c 100644 --- a/updates/latest.go +++ b/updates/latest.go @@ -11,6 +11,7 @@ import ( "sync" "github.com/safing/portbase/log" + "github.com/safing/portbase/utils" semver "github.com/hashicorp/go-version" ) @@ -165,3 +166,39 @@ func LoadIndexes() error { return nil } + +// CreateSymlinks creates a directory structure with unversions symlinks to the given updates list. +func CreateSymlinks(symlinkRoot, updateStorage *utils.DirStructure, updatesList map[string]string) error { + err := os.RemoveAll(symlinkRoot.Path) + if err != nil { + return fmt.Errorf("failed to wipe symlink root: %s", err) + } + + err = symlinkRoot.Ensure() + if err != nil { + return fmt.Errorf("failed to create symlink root: %s", err) + } + + for identifier, version := range updatesList { + targetPath := filepath.Join(updateStorage.Path, filepath.FromSlash(GetVersionedPath(identifier, version))) + linkPath := filepath.Join(symlinkRoot.Path, filepath.FromSlash(identifier)) + linkPathDir := filepath.Dir(linkPath) + + err = symlinkRoot.EnsureAbsPath(linkPathDir) + if err != nil { + return fmt.Errorf("failed to create dir for link: %s", err) + } + + relativeTargetPath, err := filepath.Rel(linkPathDir, targetPath) + if err != nil { + return fmt.Errorf("failed to get relative target path: %s", err) + } + + err = os.Symlink(relativeTargetPath, linkPath) + if err != nil { + return fmt.Errorf("failed to link %s: %s", identifier, err) + } + } + + return nil +} diff --git a/updates/uptool/root.go b/updates/uptool/root.go index 8c75312c..f05c3a08 100644 --- a/updates/uptool/root.go +++ b/updates/uptool/root.go @@ -1,23 +1,38 @@ package main import ( - "fmt" "os" + "path/filepath" + + "github.com/safing/portbase/utils" "github.com/spf13/cobra" ) +var ( + updatesStorage *utils.DirStructure +) + var rootCmd = &cobra.Command{ Use: "uptool", Short: "helper tool for the update process", Run: func(cmd *cobra.Command, args []string) { cmd.Usage() }, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + absPath, err := filepath.Abs(".") + if err != nil { + return err + } + + updatesStorage = utils.NewDirStructure(absPath, 0755) + return nil + }, + SilenceUsage: true, } func main() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) os.Exit(1) } } diff --git a/updates/uptool/update.go b/updates/uptool/update.go new file mode 100644 index 00000000..6e63fd77 --- /dev/null +++ b/updates/uptool/update.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + + "github.com/safing/portmaster/updates" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(updateCmd) +} + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update scans the current directory and updates the index and symlink structure", + RunE: update, +} + +func update(cmd *cobra.Command, args []string) error { + + latest, err := updates.ScanForLatest(".", true) + if err != nil { + return err + } + + data, err := json.MarshalIndent(latest, "", " ") + if err != nil { + return err + } + + err = ioutil.WriteFile("stable.json", data, 0755) + if err != nil { + return err + } + + err = updates.CreateSymlinks(updatesStorage.ChildDir("latest", 0755), updatesStorage, latest) + if err != nil { + return err + } + + return nil +}