Repository: ariga/atlas
Pull request #303 (2021-12-12 16:19:16)

cmd/atlas: expose actions package


Patch:
diff --git a/cmd/internal/base/apply.go b/cmd/action/apply.go
similarity index 65%
rename from cmd/internal/base/apply.go
rename to cmd/action/apply.go
index 8674e8da..35706fdf 100644
--- a/cmd/internal/base/apply.go
+++ b/cmd/action/apply.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"context"
@@ -12,20 +12,17 @@ import (
 )
 
 var (
-	applyFlags struct {
-		dsn  string
-		file string
+	// ApplyFlags are the flags used in Apply command.
+	ApplyFlags struct {
+		DSN  string
+		File string
+		Web  bool
 	}
-	// applyCmd represents the apply command.
-	applyCmd = &cobra.Command{
+	// ApplyCmd represents the apply command.
+	ApplyCmd = &cobra.Command{
 		Use:   "apply",
 		Short: "Apply an atlas schema to a data source",
-		Run: func(cmd *cobra.Command, args []string) {
-			d, err := defaultMux.OpenAtlas(applyFlags.dsn)
-			cobra.CheckErr(err)
-			u := schemaUnmarshal{unmarshalSpec: d.UnmarshalSpec, unmarshaler: schemahcl.Unmarshal}
-			applyRun(d, &u, applyFlags.dsn, applyFlags.file)
-		},
+		Run:   CmdApplyRun,
 		Example: `
 atlas schema apply -d mysql://user:pass@tcp(localhost:3306)/dbname -f atlas.hcl
 atlas schema apply --dsn postgres://user:pass@host:port/dbname -f atlas.hcl`,
@@ -38,11 +35,20 @@ const (
 )
 
 func init() {
-	schemaCmd.AddCommand(applyCmd)
-	applyCmd.Flags().StringVarP(&applyFlags.dsn, "dsn", "d", "", "[driver://username:password@protocol(address)/dbname?param=value] Select data source using the dsn format")
-	applyCmd.Flags().StringVarP(&applyFlags.file, "file", "f", "", "[/path/to/file] file containing schema")
-	cobra.CheckErr(applyCmd.MarkFlagRequired("dsn"))
-	cobra.CheckErr(applyCmd.MarkFlagRequired("file"))
+	schemaCmd.AddCommand(ApplyCmd)
+	ApplyCmd.Flags().StringVarP(&ApplyFlags.DSN, "dsn", "d", "", "[driver://username:password@protocol(address)/dbname?param=value] Select data source using the dsn format")
+	ApplyCmd.Flags().StringVarP(&ApplyFlags.File, "file", "f", "", "[/path/to/file] file containing schema")
+	ApplyCmd.Flags().BoolVarP(&ApplyFlags.Web, "web", "w", false, "open in UI server")
+	cobra.CheckErr(ApplyCmd.MarkFlagRequired("dsn"))
+	cobra.CheckErr(ApplyCmd.MarkFlagRequired("file"))
+}
+
+// CmdApplyRun is the command used when running CLI.
+func CmdApplyRun(cmd *cobra.Command, args []string) {
+	d, err := defaultMux.OpenAtlas(ApplyFlags.DSN)
+	cobra.CheckErr(err)
+	u := schemaUnmarshal{unmarshalSpec: d.UnmarshalSpec, unmarshaler: schemahcl.Unmarshal}
+	applyRun(d, &u, ApplyFlags.DSN, ApplyFlags.File)
 }
 
 func applyRun(d *Driver, u schemaUnmarshaler, dsn string, file string) {
diff --git a/cmd/internal/base/diff.go b/cmd/action/diff.go
similarity index 99%
rename from cmd/internal/base/diff.go
rename to cmd/action/diff.go
index 186fc810..21e26c8f 100644
--- a/cmd/internal/base/diff.go
+++ b/cmd/action/diff.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"context"
diff --git a/cmd/internal/base/inspect.go b/cmd/action/inspect.go
similarity index 98%
rename from cmd/internal/base/inspect.go
rename to cmd/action/inspect.go
index 75c37788..84472766 100644
--- a/cmd/internal/base/inspect.go
+++ b/cmd/action/inspect.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"context"
diff --git a/cmd/internal/base/mux.go b/cmd/action/mux.go
similarity index 99%
rename from cmd/internal/base/mux.go
rename to cmd/action/mux.go
index e73cb6a5..0a51f5a0 100644
--- a/cmd/internal/base/mux.go
+++ b/cmd/action/mux.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"context"
diff --git a/cmd/internal/base/provider.go b/cmd/action/provider.go
similarity index 98%
rename from cmd/internal/base/provider.go
rename to cmd/action/provider.go
index d7a32951..0f5387a4 100644
--- a/cmd/internal/base/provider.go
+++ b/cmd/action/provider.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"database/sql"
diff --git a/cmd/internal/base/root.go b/cmd/action/root.go
similarity index 92%
rename from cmd/internal/base/root.go
rename to cmd/action/root.go
index dfab3db2..8d1111a0 100644
--- a/cmd/internal/base/root.go
+++ b/cmd/action/root.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import "github.com/spf13/cobra"
 
diff --git a/cmd/internal/base/schema.go b/cmd/action/schema.go
similarity index 94%
rename from cmd/internal/base/schema.go
rename to cmd/action/schema.go
index 4fe197ba..9538d6a5 100644
--- a/cmd/internal/base/schema.go
+++ b/cmd/action/schema.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"github.com/spf13/cobra"
diff --git a/cmd/atlas/doc.go b/cmd/atlas/doc.go
index b5eefc22..d0fe3b89 100644
--- a/cmd/atlas/doc.go
+++ b/cmd/atlas/doc.go
@@ -8,7 +8,7 @@ import (
 	"os"
 	"path"
 
-	"ariga.io/atlas/cmd/internal/base"
+	"ariga.io/atlas/cmd/action"
 	"github.com/spf13/cobra/doc"
 )
 
@@ -18,8 +18,8 @@ func main() {
 		log.Fatal(err)
 	}
 	d := path.Join(pwd, "../../doc/md/CLI")
-	base.RootCmd.DisableAutoGenTag = true
-	err = doc.GenMarkdownTree(base.RootCmd, d)
+	action.RootCmd.DisableAutoGenTag = true
+	err = doc.GenMarkdownTree(action.RootCmd, d)
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/cmd/atlas/main.go b/cmd/atlas/main.go
index 6d2a9fc1..8cdb2051 100644
--- a/cmd/atlas/main.go
+++ b/cmd/atlas/main.go
@@ -3,7 +3,7 @@ package main
 import (
 	"os"
 
-	"ariga.io/atlas/cmd/internal/base"
+	"ariga.io/atlas/cmd/action"
 	_ "github.com/go-sql-driver/mysql"
 	_ "github.com/lib/pq"
 	"github.com/spf13/cobra"
@@ -11,10 +11,10 @@ import (
 
 func main() {
 	cobra.OnInitialize(initConfig)
-	cobra.CheckErr(base.RootCmd.Execute())
+	cobra.CheckErr(action.RootCmd.Execute())
 }
 
 // initConfig reads in config file and ENV variables if set.
 func initConfig() {
-	base.RootCmd.SetOut(os.Stdout)
+	action.RootCmd.SetOut(os.Stdout)
 }
diff --git a/doc/md/CLI/atlas_schema_apply.md b/doc/md/CLI/atlas_schema_apply.md
index 8389bb91..9e8b2830 100644
--- a/doc/md/CLI/atlas_schema_apply.md
+++ b/doc/md/CLI/atlas_schema_apply.md
@@ -20,6 +20,7 @@ atlas schema apply --dsn postgres://user:pass@host:port/dbname -f atlas.hcl
   -d, --dsn string    [driver://username:password@protocol(address)/dbname?param=value] Select data source using the dsn format
   -f, --file string   [/path/to/file] file containing schema
   -h, --help          help for apply
+  -w, --web           open in UI server
 ```
 
 ### SEE ALSO
diff --git a/go.sum b/go.sum
index e31765bc..922c2ab1 100644
--- a/go.sum
+++ b/go.sum
@@ -67,6 +67,7 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
 github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
 github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
 github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
+github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
 github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -231,11 +232,13 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
 github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
+github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
 github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
 github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
 github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
+github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
 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=
@@ -612,6 +615,7 @@ gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=


Test patch:
diff --git a/cmd/internal/base/diff_test.go b/cmd/action/diff_test.go
similarity index 99%
rename from cmd/internal/base/diff_test.go
rename to cmd/action/diff_test.go
index 22327fbc..19bf4fc8 100644
--- a/cmd/internal/base/diff_test.go
+++ b/cmd/action/diff_test.go
@@ -1,4 +1,4 @@
-package base
+package action
 
 import (
 	"context"
diff --git a/cmd/internal/base/mux_test.go b/cmd/action/mux_test.go
similarity index 66%
rename from cmd/internal/base/mux_test.go
rename to cmd/action/mux_test.go
index 2c1fd126..14d9c58f 100644
--- a/cmd/internal/base/mux_test.go
+++ b/cmd/action/mux_test.go
@@ -1,40 +1,40 @@
-package base_test
+package action_test
 
 import (
 	"testing"
 
-	"ariga.io/atlas/cmd/internal/base"
+	"ariga.io/atlas/cmd/action"
 	"github.com/stretchr/testify/require"
 )
 
 func Test_ProviderNotSupported(t *testing.T) {
-	u := base.NewMux()
+	u := action.NewMux()
 	_, err := u.OpenAtlas("fake://open")
 	require.Error(t, err)
 }
 
 func Test_RegisterProvider(t *testing.T) {
-	u := base.NewMux()
-	p := func(s string) (*base.Driver, error) { return nil, nil }
+	u := action.NewMux()
+	p := func(s string) (*action.Driver, error) { return nil, nil }
 	require.NotPanics(t, func() { u.RegisterProvider("key", p) })
 }
 
 func Test_RegisterTwiceSameKeyFails(t *testing.T) {
-	u := base.NewMux()
-	p := func(s string) (*base.Driver, error) { return nil, nil }
+	u := action.NewMux()
+	p := func(s string) (*action.Driver, error) { return nil, nil }
 	require.NotPanics(t, func() { u.RegisterProvider("key", p) })
 	require.Panics(t, func() { u.RegisterProvider("key", p) })
 }
 
 func Test_GetDriverFails(t *testing.T) {
-	u := base.NewMux()
+	u := action.NewMux()
 	_, err := u.OpenAtlas("key://open")
 	require.Error(t, err)
 }
 
 func Test_GetDriverSuccess(t *testing.T) {
-	u := base.NewMux()
-	p := func(s string) (*base.Driver, error) { return nil, nil }
+	u := action.NewMux()
+	p := func(s string) (*action.Driver, error) { return nil, nil }
 	u.RegisterProvider("key", p)
 	_, err := u.OpenAtlas("key://open")
 	require.NoError(t, err)