From 5eae1299f7865c8cfb27b2fe3dfeb7611d0406c8 Mon Sep 17 00:00:00 2001 From: Sung <8265228+sungwoncho@users.noreply.github.com> Date: Sat, 27 May 2023 17:49:34 +1000 Subject: [PATCH] Allow to disable automatic upgrade check in CLI (#646) --- pkg/cli/config/config.go | 5 +-- pkg/cli/context/ctx.go | 17 +++++----- pkg/cli/infra/init.go | 22 +++++++------ pkg/cli/migrate/migrate.go | 2 ++ pkg/cli/migrate/migrate_test.go | 56 ++++++++++++++++++++++++++++----- pkg/cli/migrate/migrations.go | 19 +++++++++++ pkg/cli/upgrade/upgrade.go | 5 +++ 7 files changed, 99 insertions(+), 27 deletions(-) diff --git a/pkg/cli/config/config.go b/pkg/cli/config/config.go index e12c5c45..f1d5e02e 100644 --- a/pkg/cli/config/config.go +++ b/pkg/cli/config/config.go @@ -32,8 +32,9 @@ import ( // Config holds dnote configuration type Config struct { - Editor string `yaml:"editor"` - APIEndpoint string `yaml:"apiEndpoint"` + Editor string `yaml:"editor"` + APIEndpoint string `yaml:"apiEndpoint"` + EnableUpgradeCheck bool `yaml:"enableUpgradeCheck"` } func checkLegacyPath(ctx context.DnoteCtx) (string, bool) { diff --git a/pkg/cli/context/ctx.go b/pkg/cli/context/ctx.go index cfef776b..6fa7f726 100644 --- a/pkg/cli/context/ctx.go +++ b/pkg/cli/context/ctx.go @@ -35,14 +35,15 @@ type Paths struct { // DnoteCtx is a context holding the information of the current runtime type DnoteCtx struct { - Paths Paths - APIEndpoint string - Version string - DB *database.DB - SessionKey string - SessionKeyExpiry int64 - Editor string - Clock clock.Clock + Paths Paths + APIEndpoint string + Version string + DB *database.DB + SessionKey string + SessionKeyExpiry int64 + Editor string + Clock clock.Clock + EnableUpgradeCheck bool } // Redact replaces private information from the context with a set of diff --git a/pkg/cli/infra/init.go b/pkg/cli/infra/init.go index fc767e6b..be748129 100644 --- a/pkg/cli/infra/init.go +++ b/pkg/cli/infra/init.go @@ -150,14 +150,15 @@ func SetupCtx(ctx context.DnoteCtx) (context.DnoteCtx, error) { } ret := context.DnoteCtx{ - Paths: ctx.Paths, - Version: ctx.Version, - DB: ctx.DB, - SessionKey: sessionKey, - SessionKeyExpiry: sessionKeyExpiry, - APIEndpoint: cf.APIEndpoint, - Editor: cf.Editor, - Clock: clock.New(), + Paths: ctx.Paths, + Version: ctx.Version, + DB: ctx.DB, + SessionKey: sessionKey, + SessionKeyExpiry: sessionKeyExpiry, + APIEndpoint: cf.APIEndpoint, + Editor: cf.Editor, + Clock: clock.New(), + EnableUpgradeCheck: cf.EnableUpgradeCheck, } return ret, nil @@ -354,8 +355,9 @@ func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error { editor := getEditorCommand() cf := config.Config{ - Editor: editor, - APIEndpoint: apiEndpoint, + Editor: editor, + APIEndpoint: apiEndpoint, + EnableUpgradeCheck: true, } if err := config.Write(ctx, cf); err != nil { diff --git a/pkg/cli/migrate/migrate.go b/pkg/cli/migrate/migrate.go index d7a3936a..ea6c9d57 100644 --- a/pkg/cli/migrate/migrate.go +++ b/pkg/cli/migrate/migrate.go @@ -20,6 +20,7 @@ package migrate import ( "database/sql" + "github.com/dnote/dnote/pkg/cli/consts" "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/log" @@ -47,6 +48,7 @@ var LocalSequence = []migration{ lm10, lm11, lm12, + lm13, } // RemoteSequence is a list of remote migrations to be run diff --git a/pkg/cli/migrate/migrate_test.go b/pkg/cli/migrate/migrate_test.go index c5e2a717..7ce2e345 100644 --- a/pkg/cli/migrate/migrate_test.go +++ b/pkg/cli/migrate/migrate_test.go @@ -21,13 +21,14 @@ package migrate import ( "encoding/json" "fmt" - "gopkg.in/yaml.v2" "io/ioutil" "net/http" "net/http/httptest" "testing" "time" + "gopkg.in/yaml.v2" + "github.com/dnote/actions" "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/cli/consts" @@ -38,11 +39,10 @@ import ( ) var paths context.Paths = context.Paths{ - Home: "../../tmp", - Cache: "../../tmp", - Config: "../../tmp", - Data: "../../tmp", - LegacyDnote: "../../tmp", + Home: "../../tmp", + Cache: "../../tmp", + Config: "../../tmp", + Data: "../../tmp", } func TestExecute_bump_schema(t *testing.T) { @@ -1078,7 +1078,7 @@ func TestLocalMigration12(t *testing.T) { defer context.TeardownTestCtx(t, ctx) data := []byte("editor: vim") - path := fmt.Sprintf("%s/dnoterc", ctx.Paths.LegacyDnote) + path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName) if err := ioutil.WriteFile(path, data, 0644); err != nil { t.Fatal(errors.Wrap(err, "Failed to write schema file")) } @@ -1108,6 +1108,48 @@ func TestLocalMigration12(t *testing.T) { assert.NotEqual(t, cf.APIEndpoint, "", "apiEndpoint was not populated") } +func TestLocalMigration13(t *testing.T) { + // set up + opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-12-pre-schema.sql", SkipMigration: true} + ctx := context.InitTestCtx(t, paths, &opts) + defer context.TeardownTestCtx(t, ctx) + + data := []byte("editor: vim\napiEndpoint: https://test.com/api") + + path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName) + if err := ioutil.WriteFile(path, data, 0644); err != nil { + t.Fatal(errors.Wrap(err, "Failed to write schema file")) + } + + // execute + err := lm13.run(ctx, nil) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + b, err := ioutil.ReadFile(path) + if err != nil { + t.Fatal(errors.Wrap(err, "reading config")) + } + + type config struct { + Editor string `yaml:"editor"` + ApiEndpoint string `yaml:"apiEndpoint"` + EnableUpgradeCheck bool `yaml:"enableUpgradeCheck"` + } + + var cf config + err = yaml.Unmarshal(b, &cf) + if err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling config")) + } + + assert.Equal(t, cf.Editor, "vim", "editor mismatch") + assert.Equal(t, cf.ApiEndpoint, "https://test.com/api", "apiEndpoint mismatch") + assert.Equal(t, cf.EnableUpgradeCheck, true, "enableUpgradeCheck mismatch") +} + func TestRemoteMigration1(t *testing.T) { // set up opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/remote-1-pre-schema.sql", SkipMigration: true} diff --git a/pkg/cli/migrate/migrations.go b/pkg/cli/migrate/migrations.go index 254de773..259b661d 100644 --- a/pkg/cli/migrate/migrations.go +++ b/pkg/cli/migrate/migrations.go @@ -550,6 +550,25 @@ var lm12 = migration{ }, } +var lm13 = migration{ + name: "add enableUpgradeCheck to the configuration file", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + cf, err := config.Read(ctx) + if err != nil { + return errors.Wrap(err, "reading config") + } + + cf.EnableUpgradeCheck = true + + err = config.Write(ctx, cf) + if err != nil { + return errors.Wrap(err, "writing config") + } + + return nil + }, +} + var rm1 = migration{ name: "sync-book-uuids-from-server", run: func(ctx context.DnoteCtx, tx *database.DB) error { diff --git a/pkg/cli/upgrade/upgrade.go b/pkg/cli/upgrade/upgrade.go index 62e29d91..596c6a87 100644 --- a/pkg/cli/upgrade/upgrade.go +++ b/pkg/cli/upgrade/upgrade.go @@ -112,6 +112,11 @@ func checkVersion(ctx context.DnoteCtx) error { // Check triggers update if needed func Check(ctx context.DnoteCtx) error { + // If upgrade check is not enabled, do not proceed further + if !ctx.EnableUpgradeCheck { + return nil + } + shouldCheck, err := shouldCheckUpdate(ctx) if err != nil { return errors.Wrap(err, "checking if dnote should check update")