Allow to disable automatic upgrade check in CLI (#646)

This commit is contained in:
Sung 2023-05-27 17:49:34 +10:00 committed by GitHub
commit 5eae1299f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 99 additions and 27 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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