Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address profile data loss #590

Merged
merged 18 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ SHELL := /bin/bash -o pipefail
# the output is consistent across environments.
VERSION ?= $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)

# allows for passing additional args to the build e.g.
# make build GO_ARGS='--ldflags "-s -w"'
GO_ARGS ?= ""
# Allows overriding go executable.
GO_BIN ?= go
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this lets me validate changes across different versions of go.


# Enables support for tools such as https://github.com/rakyll/gotest
TEST_COMMAND ?= go test
TEST_COMMAND ?= $(GO_BIN) test

# The compute tests can sometimes exceed the default 10m limit.
TEST_ARGS ?= -timeout 15m ./{cmd,pkg}/...
Expand All @@ -19,37 +18,36 @@ CLI_ENV ?= "development"

GOHOSTOS ?= $(shell go env GOHOSTOS || echo unknown)
GOHOSTARCH ?= $(shell go env GOHOSTARCH || echo unknown)

GO_FILES = $(shell find cmd pkg -type f -name '*.go')
GO_FILES = $(shell find cmd pkg -type f -name '*.go')

# You can pass flags to goreleaser via GORELEASER_ARGS
# --skip-validate will skip the checks
# --rm-dist will save you deleting the dist dir
# --single-target will be quicker and only build for your os & architecture
# e.g.
# make fastly GORELEASER_ARGS="--skip-validate --rm-dist"
fastly: $(GO_FILES)
fastly: dependencies $(GO_FILES)
@GOHOSTOS="${GOHOSTOS}" GOHOSTARCH="${GOHOSTARCH}" goreleaser build ${GORELEASER_ARGS}


# useful for attaching a debugger such as https://github.com/go-delve/delve
debug:
@go build -gcflags="all=-N -l" $(GO_ARGS) -o "fastly" ./cmd/fastly
@$(GO_BIN) build -gcflags="all=-N -l" $(GO_ARGS) -o "fastly" ./cmd/fastly

.PHONY: all
all: dependencies config tidy fmt vet staticcheck gosec test build install

# update goreleaser inline with the release GHA workflow
.PHONY: dependencies
dependencies:
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
go install github.com/mgechev/revive@latest
go install github.com/goreleaser/[email protected]
$(GO_BIN) install github.com/securego/gosec/v2/cmd/gosec@latest
$(GO_BIN) install honnef.co/go/tools/cmd/staticcheck@latest
$(GO_BIN) install github.com/mgechev/revive@latest
$(GO_BIN) install github.com/goreleaser/[email protected]

.PHONY: tidy
tidy:
go mod tidy
$(GO_BIN) mod tidy

.PHONY: fmt
fmt:
Expand All @@ -58,7 +56,7 @@ fmt:

.PHONY: vet
vet:
go vet ./{cmd,pkg}/...
$(GO_BIN) vet ./{cmd,pkg}/...

.PHONY: revive
revive:
Expand All @@ -76,13 +74,16 @@ staticcheck:
test: config
@$(TEST_COMMAND) -race $(TEST_ARGS)

# GO_ARGS allows for passing additional args to the build e.g.
# make build GO_ARGS='--ldflags "-s -w"'
.PHONY: build
build: config
go build $(GO_ARGS) ./cmd/fastly
$(GO_BIN) build $(GO_ARGS) ./cmd/fastly

# GO_ARGS allows for passing additional args to the build e.g.
.PHONY: install
install: config
go install $(GO_ARGS) ./cmd/fastly
$(GO_BIN) install $(GO_ARGS) ./cmd/fastly

.PHONY: changelog
changelog:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ require (
)

require (
github.com/gofrs/flock v0.8.1
github.com/otiai10/copy v1.7.0
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/sethvargo/go-retry v0.2.3
github.com/tcnksm/go-gitconfig v0.1.2
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AE
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -208,6 +210,8 @@ github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtm
github.com/segmentio/textio v1.2.0 h1:Ug4IkV3kh72juJbG8azoSBlgebIbUUxVNrfFcKHfTSQ=
github.com/segmentio/textio v1.2.0/go.mod h1:+Rb7v0YVODP+tK5F7FD9TCkV7gOYx9IgLHWiqtvY8ag=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sethvargo/go-retry v0.2.3 h1:oYlgvIvsju3jNbottWABtbnoLC+GDtLdBHxKWxQm/iU=
github.com/sethvargo/go-retry v0.2.3/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
Expand Down
22 changes: 14 additions & 8 deletions pkg/commands/profile/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,29 @@ func (c *CreateCommand) validateToken(token, endpoint string, progress text.Prog
func (c *CreateCommand) updateInMemCfg(profileName, email, token, endpoint string, def bool, progress text.Progress) {
progress.Step("Persisting configuration...")

c.Globals.File.Fastly.APIEndpoint = endpoint
config.Mutex.Lock()
{
c.Globals.File.Fastly.APIEndpoint = endpoint

if c.Globals.File.Profiles == nil {
c.Globals.File.Profiles = make(config.Profiles)
}
c.Globals.File.Profiles[profileName] = &config.Profile{
Default: def,
Email: email,
Token: token,
if c.Globals.File.Profiles == nil {
c.Globals.File.Profiles = make(config.Profiles)
}
c.Globals.File.Profiles[profileName] = &config.Profile{
Default: def,
Email: email,
Token: token,
}
}
config.Mutex.Unlock()

// If the user wants the newly created profile to be their new default, then
// we'll call Set for its side effect of resetting all other profiles to have
// their Default field set to false.
if def {
if p, ok := profile.Set(profileName, c.Globals.File.Profiles); ok {
config.Mutex.Lock()
c.Globals.File.Profiles = p
config.Mutex.Unlock()
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/commands/profile/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ func NewSwitchCommand(parent cmd.Registerer, globals *config.Data) *SwitchComman
func (c *SwitchCommand) Exec(in io.Reader, out io.Writer) error {
var ok bool

if c.Globals.File.Profiles, ok = profile.Set(c.profile, c.Globals.File.Profiles); !ok {
p, ok := profile.Set(c.profile, c.Globals.File.Profiles)
if !ok {
msg := fmt.Sprintf(profile.DoesNotExist, c.profile)
err := errors.New(msg)
c.Globals.ErrLog.Add(err)
return err
}

config.Mutex.Lock()
c.Globals.File.Profiles = p
config.Mutex.Unlock()

if err := c.Globals.File.Write(c.Globals.Path); err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error saving config file: %w", err)
Expand Down
12 changes: 11 additions & 1 deletion pkg/commands/profile/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
}
if token != "" {
opts = append(opts, func(p *config.Profile) {
config.Mutex.Lock()
p.Token = token
config.Mutex.Unlock()
})
}

Expand All @@ -72,7 +74,9 @@ func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
return err
}
opts = append(opts, func(p *config.Profile) {
config.Mutex.Lock()
p.Default = def
config.Mutex.Unlock()
})

// User didn't want to change their token value so reassign original.
Expand All @@ -98,18 +102,24 @@ func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
return err
}
opts = append(opts, func(p *config.Profile) {
config.Mutex.Lock()
p.Email = u.Login
config.Mutex.Unlock()
})

var ok bool

if c.Globals.File.Profiles, ok = profile.Edit(c.profile, c.Globals.File.Profiles, opts...); !ok {
ps, ok := profile.Edit(c.profile, c.Globals.File.Profiles, opts...)
if !ok {
msg := fmt.Sprintf(profile.DoesNotExist, c.profile)
return fsterr.RemediationError{
Inner: fmt.Errorf(msg),
Remediation: fsterr.ProfileRemediation,
}
}
config.Mutex.Lock()
c.Globals.File.Profiles = ps
config.Mutex.Unlock()

if err := c.persistCfg(); err != nil {
return err
Expand Down
Loading