-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/atlascmd: support project file in inspect (#771)
- Loading branch information
Showing
7 changed files
with
329 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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 atlascmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"ariga.io/atlas/schema/schemaspec" | ||
"ariga.io/atlas/schema/schemaspec/schemahcl" | ||
) | ||
|
||
const projectFileName = "atlas.hcl" | ||
|
||
// projectFile represents an atlas.hcl file. | ||
type projectFile struct { | ||
Envs []*Env `spec:"env"` | ||
} | ||
|
||
// Env represents an Atlas environment. | ||
type Env struct { | ||
// Name for this environment. | ||
Name string `spec:"name,name"` | ||
|
||
// URL of the database. | ||
URL string `spec:"url"` | ||
|
||
// URL of the dev-database for this environment. | ||
// See: https://atlasgo.io/dev-database | ||
DevURL string `spec:"dev"` | ||
|
||
// Path to the file containing the desired schema of the environment. | ||
Source string `spec:"src"` | ||
|
||
// List of schemas in this database that are managed by Atlas. | ||
Schemas []string `spec:"schemas"` | ||
schemaspec.DefaultExtension | ||
} | ||
|
||
// LoadEnv reads the project file in path, and loads the environment | ||
// with the provided name into env. | ||
func LoadEnv(path string, name string) (*Env, error) { | ||
b, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var project projectFile | ||
if err := schemahcl.New().Eval(b, &project, nil); err != nil { | ||
return nil, fmt.Errorf("error reading project file: %w", err) | ||
} | ||
projEnvs := make(map[string]*Env) | ||
for _, e := range project.Envs { | ||
if _, ok := projEnvs[e.Name]; ok { | ||
return nil, fmt.Errorf("duplicate environment name %q", e.Name) | ||
} | ||
if e.Name == "" { | ||
return nil, fmt.Errorf("all envs must have names on file %q", path) | ||
} | ||
if e.URL == "" { | ||
return nil, fmt.Errorf("no url set for e %q", e.Name) | ||
} | ||
projEnvs[e.Name] = e | ||
} | ||
selected, ok := projEnvs[name] | ||
if !ok { | ||
return nil, fmt.Errorf("env %q not defined in project file", name) | ||
} | ||
return selected, nil | ||
} | ||
|
||
func init() { | ||
schemaspec.Register("env", &Env{}) | ||
} |
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,58 @@ | ||
// 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 atlascmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadEnv(t *testing.T) { | ||
d := t.TempDir() | ||
h := ` | ||
env "local" { | ||
url = "mysql://root:pass@localhost:3306/" | ||
dev = "docker://mysql/8" | ||
src = "./app.hcl" | ||
schemas = ["hello", "world"] | ||
} | ||
` | ||
err := os.WriteFile(filepath.Join(d, projectFileName), []byte(h), 0600) | ||
require.NoError(t, err) | ||
path := filepath.Join(d, projectFileName) | ||
t.Run("ok", func(t *testing.T) { | ||
env := &Env{} | ||
env, err = LoadEnv(path, "local") | ||
require.NoError(t, err) | ||
require.EqualValues(t, &Env{ | ||
Name: "local", | ||
URL: "mysql://root:pass@localhost:3306/", | ||
DevURL: "docker://mysql/8", | ||
Source: "./app.hcl", | ||
Schemas: []string{"hello", "world"}, | ||
}, env) | ||
}) | ||
t.Run("wrong env", func(t *testing.T) { | ||
_, err = LoadEnv(path, "home") | ||
require.EqualError(t, err, `env "home" not defined in project file`) | ||
}) | ||
t.Run("wrong dir", func(t *testing.T) { | ||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
_, err = LoadEnv(filepath.Join(wd, projectFileName), "home") | ||
require.ErrorContains(t, err, `no such file or directory`) | ||
}) | ||
t.Run("duplicate env", func(t *testing.T) { | ||
dup := h + "\n" + h | ||
path := filepath.Join(d, "dup.hcl") | ||
err = os.WriteFile(path, []byte(dup), 0600) | ||
require.NoError(t, err) | ||
_, err = LoadEnv(path, "local") | ||
require.EqualError(t, err, `duplicate environment name "local"`) | ||
}) | ||
} |
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
Oops, something went wrong.