Skip to content

Commit

Permalink
Introduce NativeCore
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed May 14, 2018
1 parent 662fee6 commit 9c29ce5
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 126 deletions.
3 changes: 3 additions & 0 deletions cl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ type CLI struct {
Localizer *localize.Localizer

PreferLaunch bool
Upgrade bool
Uninstall bool
Relaunch bool
RelaunchPID int

Silent bool
}
65 changes: 64 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

"github.com/cloudfoundry-attic/jibber_jabber"
"github.com/itchio/itch-setup/bindata"
"github.com/itchio/itch-setup/cl"
"github.com/itchio/itch-setup/localize"
"github.com/itchio/itch-setup/native"
"github.com/pkg/errors"

kingpin "gopkg.in/alecthomas/kingpin.v2"
)
Expand All @@ -30,11 +32,17 @@ var cli cl.CLI

func init() {
app.Flag("prefer-launch", "Launch if a valid version of itch is installed").BoolVar(&cli.PreferLaunch)

app.Flag("upgrade", "Upgrade the itch app if necessary").BoolVar(&cli.Upgrade)

app.Flag("uninstall", "Uninstall the itch app").BoolVar(&cli.Uninstall)

app.Flag("relaunch", "Relaunch a new version of the itch app").BoolVar(&cli.Relaunch)
app.Flag("relaunch-pid", "PID to wait for before relaunching").IntVar(&cli.RelaunchPID)

app.Flag("appname", "Application name (itch or kitch)").StringVar(&cli.AppName)

app.Flag("silent", "Run installation silently").BoolVar(&cli.Silent)
}

func must(err error) {
Expand Down Expand Up @@ -121,5 +129,60 @@ func main() {
}
cli.Localizer = localizer

native.Do(cli)
nc, err := native.NewNativeCore(cli)
if err != nil {
panic(err)
}

var verbs []string

if cli.Upgrade {
verbs = append(verbs, "upgrade")
}
if cli.Relaunch {
verbs = append(verbs, "relaunch")
}
if cli.Uninstall {
verbs = append(verbs, "uninstall")
}

if len(verbs) > 1 {
nc.ErrorDialog(errors.Errorf("Cannot specify more than one verb: got %s", strings.Join(verbs, ", ")))
}

if len(verbs) == 0 {
verbs = append(verbs, "install")
}

switch verbs[0] {
case "install":
err = nc.Install()
if err != nil {
nc.ErrorDialog(err)
}
case "upgrade":
err = nc.Upgrade()
if err != nil {
jsonlBail(errors.WithMessage(err, "Fatal upgrade error"))
}
case "relaunch":
if cli.RelaunchPID <= 0 {
jsonlBail(errors.Errorf("--relaunch needs a valid --relaunch-pid (got %d)", cli.RelaunchPID))
}

err = nc.Relaunch()
if err != nil {
jsonlBail(errors.WithMessage(err, "Fatal relaunch error"))
}
case "uninstall":
err = nc.Uninstall()
if err != nil {
nc.ErrorDialog(err)
}
}
}

func jsonlBail(err error) {
// TODO: use json-lines
log.Fatalf("%+v", err)
}
22 changes: 22 additions & 0 deletions native/native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package native

type NativeCore interface {
// Perform install from scratch or heals existing installation
Install() error

// Remove existing installation (all versions)
Uninstall() error

// Looks for new versions, applies patches, signals update
// progress and whether a relaunch is needed.
Upgrade() error

// Waits for PID to exit, then opens latest version of
// the app. On macOS, moves latest to /Applications before
// launching
Relaunch() error

// Shows an error dialog (with stack trace and repo link)
// and exits afterwards.
ErrorDialog(err error)
}
Loading

0 comments on commit 9c29ce5

Please sign in to comment.