Skip to content

Commit

Permalink
fix snap installation
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 16, 2023
1 parent c227952 commit 55d8c78
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion entity/snap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package entity

type Snap struct {
Name string `yaml:"snap"`
Name string `yaml:"name"`
Classic bool `yaml:"classic"`
}
20 changes: 20 additions & 0 deletions internal/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package internal

import marecmd "github.com/femnad/mare/cmd"

func MaybeRunWithSudo(cmdStr string) error {
isRoot, err := IsUserRoot()
if err != nil {
return err
}

cmd := marecmd.Input{Command: cmdStr, Sudo: !isRoot}
_, err = marecmd.RunFormatError(cmd)
return err
}

func Run(cmdStr string) error {
cmd := marecmd.Input{Command: cmdStr}
_, err := marecmd.RunFormatError(cmd)
return err
}
2 changes: 1 addition & 1 deletion provision/osrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provision

import (
"errors"
marecmd "github.com/femnad/mare/cmd"

mapset "github.com/deckarep/golang-set/v2"

Expand All @@ -11,6 +10,7 @@ import (
"github.com/femnad/fup/internal"
"github.com/femnad/fup/precheck/unless"
"github.com/femnad/fup/precheck/when"
marecmd "github.com/femnad/mare/cmd"
)

func runUpdateCmds(cmds mapset.Set[string]) []error {
Expand Down
10 changes: 7 additions & 3 deletions provision/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ import (
"github.com/femnad/fup/common"
"github.com/femnad/fup/entity"
"github.com/femnad/fup/internal"
marecmd "github.com/femnad/mare/cmd"
)

func installSnap(snap entity.Snap) error {
err := internal.Run(fmt.Sprintf("snap list %s", snap.Name))
if err == nil {
return nil
}

internal.Log.Infof("Installing snap %s", snap.Name)
cmd := fmt.Sprintf("snap install %s", snap.Name)
if snap.Classic {
cmd += " --classic"
}
in := marecmd.Input{Command: cmd}

_, err := marecmd.RunFormatError(in)
err = internal.MaybeRunWithSudo(cmd)
if err != nil {
internal.Log.Errorf("error installing snap %s: %v", snap.Name, err)
return err
Expand Down

0 comments on commit 55d8c78

Please sign in to comment.