Add symlink creation to updates module and uptool

This commit is contained in:
Daniel 2019-08-16 16:46:20 +02:00
parent 9cbf98fc98
commit ca9834effa
3 changed files with 98 additions and 2 deletions

View file

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

View file

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

44
updates/uptool/update.go Normal file
View file

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