Skip to content

Commit

Permalink
Merge pull request #38 from asdf-vm/tb/setup-gofumpt
Browse files Browse the repository at this point in the history
chore(golang-rewrite): setup gofumpt
  • Loading branch information
Stratus3D authored May 3, 2024
2 parents 5663de9 + c2267d1 commit 4b00c9d
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 79 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ jobs:
go-version: '1.21.5'
- name: Install dependencies
run: go get .
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.57
- name: Install gofumpt for formatting
run: go install mvdan.cc/gofumpt@latest
- name: Run 'gofumpt'
run: gofumpt -l -w .
- name: Check format
run: '[ -z "$(gofmt -l ./...)" ]'
- name: Install revive for linting
run: go install github.com/mgechev/revive@latest
- name: Run 'revive'
run: revive -set_exit_status ./...
- name: Vet
run: go vet
- name: Install staticcheck for linting
Expand Down
6 changes: 0 additions & 6 deletions .golangci.yml

This file was deleted.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build: test lint

fmt:
go fmt ./...
gofumpt -l -w .

verify:
go mod verify
Expand All @@ -25,7 +26,7 @@ cover: test

lint: fmt
staticcheck -tests -show-ignored ./...
revive ./...
revive -set_exit_status ./...

vet: fmt
go vet .
Expand Down
10 changes: 5 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Package cmd contains the asdf CLI command code
package cmd

import (
"asdf/config"
"asdf/plugins"
"log"
"os"

"asdf/config"
"asdf/plugins"

"github.com/urfave/cli/v2"
)

Expand All @@ -15,6 +17,7 @@ Manage all your runtime versions with one tool!
Complete documentation is available at https://asdf-vm.com/`

// Execute defines the full CLI API and then runs it
func Execute() {
logger := log.New(os.Stderr, "", 0)
log.SetFlags(0)
Expand Down Expand Up @@ -95,7 +98,6 @@ func Execute() {
}

err := app.Run(os.Args)

if err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -129,7 +131,6 @@ func pluginRemoveCommand(_ *cli.Context, logger *log.Logger, pluginName string)
}

err = plugins.Remove(conf, pluginName)

if err != nil {
logger.Printf("error removing plugin: %s", err)
}
Expand All @@ -147,7 +148,6 @@ func pluginListCommand(cCtx *cli.Context, logger *log.Logger) error {
}

plugins, err := plugins.List(conf, urls, refs)

if err != nil {
logger.Printf("error loading plugin list: %s", err)
return err
Expand Down
62 changes: 35 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package config provides a unified API for fetching asdf config. Either from
// the asdfrc file or environment variables.
package config

import (
Expand All @@ -10,37 +12,41 @@ import (
"gopkg.in/ini.v1"
)

const ForcePrependDefault = false
const DataDirDefault = "~/.asdf"
const ConfigFileDefault = "~/.asdfrc"
const DefaultToolVersionsFilenameDefault = ".tool-versions"
const (
forcePrependDefault = false
dataDirDefault = "~/.asdf"
configFileDefault = "~/.asdfrc"
defaultToolVersionsFilenameDefault = ".tool-versions"
)

/* Struct to represent the remote plugin repo check duration (never or every N
* seconds). It's not clear to me how this should be represented in Golang so
* using a struct for maximum flexibility. */
/* PluginRepoCheckDuration represents the remote plugin repo check duration
* (never or every N seconds). It's not clear to me how this should be
* represented in Golang so using a struct for maximum flexibility. */
type PluginRepoCheckDuration struct {
Never bool
Every int
}

var PluginRepoCheckDurationDefault = PluginRepoCheckDuration{Every: 60}
var pluginRepoCheckDurationDefault = PluginRepoCheckDuration{Every: 60}

// Settings is a struct that stores config values from the asdfrc file
type Settings struct {
Loaded bool
LegacyVersionFile bool
// I don't think this setting should be supported in the Golang implementation
//UseReleaseCandidates bool
// UseReleaseCandidates bool
AlwaysKeepDownload bool
PluginRepositoryLastCheckDuration PluginRepoCheckDuration
DisablePluginShortNameRepository bool
}

// Config is the primary value this package builds and returns
type Config struct {
Home string
ConfigFile string `env:"ASDF_CONFIG_FILE, overwrite"`
DefaultToolVersionsFilename string `env:"ASDF_DEFAULT_TOOL_VERSIONS_FILENAME, overwrite"`
// Unclear if this value will be needed with the golang implementation.
//AsdfDir string
// AsdfDir string
DataDir string `env:"ASDF_DATA_DIR, overwrite"`
ForcePrepend bool `env:"ASDF_FORCE_PREPEND, overwrite"`
// Field that stores the settings struct if it is loaded
Expand All @@ -52,34 +58,33 @@ func defaultSettings() *Settings {
Loaded: false,
LegacyVersionFile: false,
AlwaysKeepDownload: false,
PluginRepositoryLastCheckDuration: PluginRepoCheckDurationDefault,
PluginRepositoryLastCheckDuration: pluginRepoCheckDurationDefault,
DisablePluginShortNameRepository: false,
}
}

func NewPluginRepoCheckDuration(checkDuration string) PluginRepoCheckDuration {
func newPluginRepoCheckDuration(checkDuration string) PluginRepoCheckDuration {
if strings.ToLower(checkDuration) == "never" {
return PluginRepoCheckDuration{Never: true}
}

every, err := strconv.Atoi(checkDuration)
if err != nil {
// if error parsing config use default value
return PluginRepoCheckDurationDefault
return pluginRepoCheckDurationDefault
}

return PluginRepoCheckDuration{Every: every}
}

// LoadConfig builds the Config struct from environment variables
func LoadConfig() (Config, error) {
config, err := loadConfigEnv()

if err != nil {
return config, err
}

homeDir, err := homedir.Dir()

if err != nil {
return config, err
}
Expand All @@ -92,39 +97,44 @@ func LoadConfig() (Config, error) {
// Methods on the Config struct that allow it to load and cache values from the
// Settings struct, which is loaded from file on disk and therefor somewhat
// "expensive".

// LegacyVersionFile loads the asdfrc if it isn't already loaded and fetches
// the legacy version file support flag
func (c *Config) LegacyVersionFile() (bool, error) {
err := c.loadSettings()

if err != nil {
return false, err
}

return c.Settings.LegacyVersionFile, nil
}

// AlwaysKeepDownload loads the asdfrc if it isn't already loaded and fetches
// the keep downloads boolean flag
func (c *Config) AlwaysKeepDownload() (bool, error) {
err := c.loadSettings()

if err != nil {
return false, err
}

return c.Settings.AlwaysKeepDownload, nil
}

// PluginRepositoryLastCheckDuration loads the asdfrc if it isn't already loaded
// and fetches the keep boolean flag
func (c *Config) PluginRepositoryLastCheckDuration() (PluginRepoCheckDuration, error) {
err := c.loadSettings()

if err != nil {
return NewPluginRepoCheckDuration(""), err
return newPluginRepoCheckDuration(""), err
}

return c.Settings.PluginRepositoryLastCheckDuration, nil
}

// DisablePluginShortNameRepository loads the asdfrc if it isn't already loaded
// and fetches the disable plugin short name repo flag
func (c *Config) DisablePluginShortNameRepository() (bool, error) {
err := c.loadSettings()

if err != nil {
return false, err
}
Expand All @@ -138,7 +148,6 @@ func (c *Config) loadSettings() error {
}

settings, err := loadSettings(c.ConfigFile)

if err != nil {
return err
}
Expand All @@ -149,21 +158,21 @@ func (c *Config) loadSettings() error {
}

func loadConfigEnv() (Config, error) {
dataDir, err := homedir.Expand(DataDirDefault)
dataDir, err := homedir.Expand(dataDirDefault)
if err != nil {
return Config{}, err
}

configFile, err := homedir.Expand(ConfigFileDefault)
configFile, err := homedir.Expand(configFileDefault)
if err != nil {
return Config{}, err
}

config := Config{
ForcePrepend: ForcePrependDefault,
ForcePrepend: forcePrependDefault,
DataDir: dataDir,
ConfigFile: configFile,
DefaultToolVersionsFilename: DefaultToolVersionsFilenameDefault,
DefaultToolVersionsFilename: defaultToolVersionsFilenameDefault,
}

context := context.Background()
Expand All @@ -175,7 +184,6 @@ func loadConfigEnv() (Config, error) {
func loadSettings(asdfrcPath string) (Settings, error) {
// asdfrc is effectively formatted as ini
config, err := ini.Load(asdfrcPath)

if err != nil {
return Settings{}, err
}
Expand All @@ -185,7 +193,7 @@ func loadSettings(asdfrcPath string) (Settings, error) {
settings := defaultSettings()

settings.Loaded = true
settings.PluginRepositoryLastCheckDuration = NewPluginRepoCheckDuration(mainConf.Key("plugin_repository_last_check_duration").String())
settings.PluginRepositoryLastCheckDuration = newPluginRepoCheckDuration(mainConf.Key("plugin_repository_last_check_duration").String())

boolOverride(&settings.LegacyVersionFile, mainConf, "legacy_version_file")
boolOverride(&settings.AlwaysKeepDownload, mainConf, "always_keep_download")
Expand Down
3 changes: 1 addition & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestBatsTests(t *testing.T) {
//})
}

//func runBatsFile(t *testing.T, dir, filename string) {
// func runBatsFile(t *testing.T, dir, filename string) {
// t.Helper()

// cmd := exec.Command("bats", "--verbose-run", fmt.Sprintf("test/%s", filename))
Expand Down Expand Up @@ -149,7 +149,6 @@ func buildAsdf(t *testing.T, dir string) {
cmd := exec.Command("go", "build", "-o", dir)

err := cmd.Run()

if err != nil {
t.Fatal("Failed to build asdf")
}
Expand Down
6 changes: 0 additions & 6 deletions plugins/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (g Plugin) Clone(pluginURL string) error {
_, err := git.PlainClone(g.directory, false, &git.CloneOptions{
URL: pluginURL,
})

if err != nil {
return fmt.Errorf("unable to clone plugin: %w", err)
}
Expand All @@ -51,7 +50,6 @@ func (g Plugin) Clone(pluginURL string) error {
// Head returns the current HEAD ref of the plugin's Git repository
func (g Plugin) Head() (string, error) {
repo, err := gitOpen(g.directory)

if err != nil {
return "", err
}
Expand All @@ -67,7 +65,6 @@ func (g Plugin) Head() (string, error) {
// RemoteURL returns the URL of the default remote for the plugin's Git repository
func (g Plugin) RemoteURL() (string, error) {
repo, err := gitOpen(g.directory)

if err != nil {
return "", err
}
Expand All @@ -84,7 +81,6 @@ func (g Plugin) RemoteURL() (string, error) {
// latest commit on the current branch
func (g Plugin) Update(ref string) (string, error) {
repo, err := gitOpen(g.directory)

if err != nil {
return "", err
}
Expand All @@ -94,7 +90,6 @@ func (g Plugin) Update(ref string) (string, error) {
if ref == "" {
// If no ref is provided checkout latest commit on current branch
head, err := repo.Head()

if err != nil {
return "", err
}
Expand Down Expand Up @@ -138,7 +133,6 @@ func (g Plugin) Update(ref string) (string, error) {

func gitOpen(directory string) (*git.Repository, error) {
repo, err := git.PlainOpen(directory)

if err != nil {
return repo, fmt.Errorf("unable to open plugin Git repository: %w", err)
}
Expand Down
Loading

0 comments on commit 4b00c9d

Please sign in to comment.