-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/atlas/internal/cmdstate: shared package for storing cmd states (#…
- Loading branch information
Showing
7 changed files
with
181 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021-present The Atlas Authors. All rights reserved. | ||
// This source code is licensed under the Apache 2.0 license found | ||
// in the LICENSE file in the root directory of this source tree. | ||
|
||
package cmdstate | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
// DefaultDir is the directory where CLI state is stored. | ||
const DefaultDir = "~/.atlas" | ||
|
||
// File is a state file for the given type. | ||
type File[T any] struct { | ||
// Dir where the file is stored. If empty, DefaultDir is used. | ||
Dir string | ||
// Name of the file. Suffixed with .json. | ||
Name string | ||
} | ||
|
||
// Read reads the value from the file system. | ||
func (f File[T]) Read() (v T, err error) { | ||
path, err := f.Path() | ||
if err != nil { | ||
return v, err | ||
} | ||
switch buf, err := os.ReadFile(path); { | ||
case os.IsNotExist(err): | ||
return newT(v), nil | ||
case err != nil: | ||
return v, err | ||
default: | ||
err = json.Unmarshal(buf, &v) | ||
return v, err | ||
} | ||
} | ||
|
||
// Write writes the value to the file system. | ||
func (f File[T]) Write(t T) error { | ||
buf, err := json.Marshal(t) | ||
if err != nil { | ||
return err | ||
} | ||
path, err := f.Path() | ||
if err != nil { | ||
return err | ||
} | ||
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { | ||
return err | ||
} | ||
return os.WriteFile(path, buf, 0666) | ||
} | ||
|
||
// Path returns the path to the file. | ||
func (f File[T]) Path() (string, error) { | ||
name := f.Name | ||
if filepath.Ext(name) == "" { | ||
name += ".json" | ||
} | ||
if f.Dir != "" { | ||
return filepath.Join(f.Dir, name), nil | ||
} | ||
path, err := homedir.Expand(filepath.Join(DefaultDir, name)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return path, nil | ||
} | ||
|
||
// newT ensures the type is initialized. | ||
func newT[T any](t T) T { | ||
if rt := reflect.TypeOf(t); rt.Kind() == reflect.Ptr { | ||
return reflect.New(rt.Elem()).Interface().(T) | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2021-present The Atlas Authors. All rights reserved. | ||
// This source code is licensed under the Apache 2.0 license found | ||
// in the LICENSE file in the root directory of this source tree. | ||
|
||
package cmdstate_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"ariga.io/atlas/cmd/atlas/internal/cmdstate" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFile(t *testing.T) { | ||
homedir.DisableCache = true | ||
t.Cleanup(func() { homedir.DisableCache = false }) | ||
|
||
type T struct{ V string } | ||
f := cmdstate.File[T]{Name: "test", Dir: t.TempDir()} | ||
v, err := f.Read() | ||
require.NoError(t, err) | ||
require.Equal(t, T{}, v) | ||
require.NoError(t, f.Write(T{V: "v"})) | ||
v, err = f.Read() | ||
require.NoError(t, err) | ||
require.Equal(t, T{V: "v"}, v) | ||
|
||
home := t.TempDir() | ||
t.Setenv("HOME", home) | ||
f = cmdstate.File[T]{Name: "t"} | ||
_, err = f.Read() | ||
require.NoError(t, err) | ||
dirs, err := os.ReadDir(home) | ||
require.NoError(t, err) | ||
require.Empty(t, dirs) | ||
|
||
require.NoError(t, f.Write(T{V: "v"})) | ||
dirs, err = os.ReadDir(home) | ||
require.NoError(t, err) | ||
require.Len(t, dirs, 1) | ||
require.Equal(t, ".atlas", dirs[0].Name()) | ||
dirs, err = os.ReadDir(filepath.Join(home, ".atlas")) | ||
require.NoError(t, err) | ||
require.Len(t, dirs, 1) | ||
require.Equal(t, "t.json", dirs[0].Name()) | ||
v, err = f.Read() | ||
require.NoError(t, err) | ||
require.Equal(t, T{V: "v"}, v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters