Skip to content

Commit ba1ad23

Browse files
authored
feat(serve): custom config option (ignite#761)
* Custom config * Custom config option * Lint * Add test for custom config file * Change option name * SOme naming * Fix test * refacto: specify file name * Lint * Suport absolute paths * Revert statik.go * Lint * Add directory skip for lint * Ilker suggestions * Verifyu config exists * COnfigPath * No config check
1 parent f0e8722 commit ba1ad23

File tree

15 files changed

+160
-46
lines changed

15 files changed

+160
-46
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
run:
22
tests: false
3+
skip-dirs:
4+
- starport/ui
35
# # timeout for analysis, e.g. 30s, 5m, default is 1m
46
# timeout: 5m
57

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/imdario/mergo v0.3.11
2828
github.com/improbable-eng/grpc-web v0.13.0
2929
github.com/jpillora/chisel v1.7.3
30+
github.com/magefile/mage v1.11.0 // indirect
3031
github.com/manifoldco/promptui v0.8.0
3132
github.com/mattn/go-zglob v0.0.3
3233
github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76
@@ -39,6 +40,7 @@ require (
3940
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40
4041
github.com/rogpeppe/go-internal v1.7.0 // indirect
4142
github.com/rs/cors v1.7.0
43+
github.com/sirupsen/logrus v1.8.0 // indirect
4244
github.com/spf13/cobra v1.1.3
4345
github.com/spf13/pflag v1.0.5
4446
github.com/stretchr/testify v1.7.0
@@ -47,6 +49,7 @@ require (
4749
golang.org/x/mod v0.4.1
4850
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
4951
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
52+
golang.org/x/sys v0.0.0-20210218085108-9555bcde0c6a // indirect
5053
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
5154
google.golang.org/grpc v1.35.0
5255
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0U
507507
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
508508
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
509509
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
510+
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
511+
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
512+
github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls=
513+
github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
510514
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
511515
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
512516
github.com/magiconair/properties v1.8.4 h1:8KGKTcQQGm0Kv7vEbKFErAoAOFyyacLStRtQSeYtvkY=
@@ -738,6 +742,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
738742
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
739743
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
740744
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
745+
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
746+
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
741747
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
742748
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
743749
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
@@ -1018,6 +1024,8 @@ golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7w
10181024
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10191025
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
10201026
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1027+
golang.org/x/sys v0.0.0-20210218085108-9555bcde0c6a h1:+Kiu2GijIw0WaCBk1i7AcqqRx8Xg3HIYaheQazXOu8w=
1028+
golang.org/x/sys v0.0.0-20210218085108-9555bcde0c6a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10211029
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
10221030
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
10231031
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=

integration/cmd_serve_test.go

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package integration_test
44

55
import (
66
"context"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
710
"testing"
811

912
"github.com/stretchr/testify/require"
@@ -14,7 +17,7 @@ func TestServeLaunchpadAppWithWasm(t *testing.T) {
1417
var (
1518
env = newEnv(t)
1619
apath = env.Scaffold("blog", Launchpad)
17-
servers = env.RandomizeServerPorts(apath)
20+
servers = env.RandomizeServerPorts(apath, "")
1821
)
1922

2023
env.Must(env.Exec("add Wasm module",
@@ -32,7 +35,7 @@ func TestServeLaunchpadAppWithWasm(t *testing.T) {
3235
defer cancel()
3336
isBackendAliveErr = env.IsAppServed(ctx, servers)
3437
}()
35-
env.Must(env.Serve("should serve with Wasm", apath, "", "", ExecCtx(ctx)))
38+
env.Must(env.Serve("should serve with Wasm", apath, "", "", "", ExecCtx(ctx)))
3639

3740
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
3841
}
@@ -41,7 +44,7 @@ func TestServeLaunchpadAppWithCustomHomes(t *testing.T) {
4144
var (
4245
env = newEnv(t)
4346
apath = env.Scaffold("blog2", Launchpad)
44-
servers = env.RandomizeServerPorts(apath)
47+
servers = env.RandomizeServerPorts(apath, "")
4548
)
4649

4750
var (
@@ -52,7 +55,7 @@ func TestServeLaunchpadAppWithCustomHomes(t *testing.T) {
5255
defer cancel()
5356
isBackendAliveErr = env.IsAppServed(ctx, servers)
5457
}()
55-
env.Must(env.Serve("should serve with Wasm", apath, "./home", "./clihome", ExecCtx(ctx)))
58+
env.Must(env.Serve("should serve with Wasm", apath, "./home", "./clihome", "", ExecCtx(ctx)))
5659

5760
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
5861
}
@@ -61,11 +64,11 @@ func TestServeLaunchpadAppWithConfigHomes(t *testing.T) {
6164
var (
6265
env = newEnv(t)
6366
apath = env.Scaffold("blog3", Launchpad)
64-
servers = env.RandomizeServerPorts(apath)
67+
servers = env.RandomizeServerPorts(apath, "")
6568
)
6669

6770
// Set config homes
68-
env.SetRandomHomeConfig(apath)
71+
env.SetRandomHomeConfig(apath, "")
6972

7073
var (
7174
ctx, cancel = context.WithTimeout(env.Ctx(), serveTimeout)
@@ -75,7 +78,7 @@ func TestServeLaunchpadAppWithConfigHomes(t *testing.T) {
7578
defer cancel()
7679
isBackendAliveErr = env.IsAppServed(ctx, servers)
7780
}()
78-
env.Must(env.Serve("should serve with Wasm", apath, "", "", ExecCtx(ctx)))
81+
env.Must(env.Serve("should serve with Wasm", apath, "", "", "", ExecCtx(ctx)))
7982

8083
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
8184
}
@@ -84,7 +87,7 @@ func TestServeStargateWithWasm(t *testing.T) {
8487
var (
8588
env = newEnv(t)
8689
apath = env.Scaffold("sgblog", Stargate)
87-
servers = env.RandomizeServerPorts(apath)
90+
servers = env.RandomizeServerPorts(apath, "")
8891
)
8992

9093
env.Must(env.Exec("add Wasm module",
@@ -102,7 +105,7 @@ func TestServeStargateWithWasm(t *testing.T) {
102105
defer cancel()
103106
isBackendAliveErr = env.IsAppServed(ctx, servers)
104107
}()
105-
env.Must(env.Serve("should serve with Stargate version", apath, "", "", ExecCtx(ctx)))
108+
env.Must(env.Serve("should serve with Stargate version", apath, "", "", "", ExecCtx(ctx)))
106109

107110
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
108111
}
@@ -111,7 +114,7 @@ func TestServeStargateWithCustomHome(t *testing.T) {
111114
var (
112115
env = newEnv(t)
113116
apath = env.Scaffold("sgblog2", Stargate)
114-
servers = env.RandomizeServerPorts(apath)
117+
servers = env.RandomizeServerPorts(apath, "")
115118
)
116119

117120
var (
@@ -122,7 +125,7 @@ func TestServeStargateWithCustomHome(t *testing.T) {
122125
defer cancel()
123126
isBackendAliveErr = env.IsAppServed(ctx, servers)
124127
}()
125-
env.Must(env.Serve("should serve with Stargate version", apath, "./home", "", ExecCtx(ctx)))
128+
env.Must(env.Serve("should serve with Stargate version", apath, "./home", "", "", ExecCtx(ctx)))
126129

127130
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
128131
}
@@ -131,11 +134,11 @@ func TestServeStargateWithConfigHome(t *testing.T) {
131134
var (
132135
env = newEnv(t)
133136
apath = env.Scaffold("sgblog3", Stargate)
134-
servers = env.RandomizeServerPorts(apath)
137+
servers = env.RandomizeServerPorts(apath, "")
135138
)
136139

137140
// Set config homes
138-
env.SetRandomHomeConfig(apath)
141+
env.SetRandomHomeConfig(apath, "")
139142

140143
var (
141144
ctx, cancel = context.WithTimeout(env.Ctx(), serveTimeout)
@@ -145,7 +148,40 @@ func TestServeStargateWithConfigHome(t *testing.T) {
145148
defer cancel()
146149
isBackendAliveErr = env.IsAppServed(ctx, servers)
147150
}()
148-
env.Must(env.Serve("should serve with Stargate version", apath, "", "", ExecCtx(ctx)))
151+
env.Must(env.Serve("should serve with Stargate version", apath, "", "", "", ExecCtx(ctx)))
152+
153+
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
154+
}
155+
156+
func TestServeStargateWithCustomConfigFile(t *testing.T) {
157+
tmpDir, err := ioutil.TempDir("", "starporttest")
158+
require.NoError(t, err)
159+
defer os.RemoveAll(tmpDir)
160+
161+
var (
162+
env = newEnv(t)
163+
apath = env.Scaffold("sgblog4", Stargate)
164+
)
165+
// Move config
166+
newConfig := "new_config.yml"
167+
newConfigPath := filepath.Join(tmpDir, newConfig)
168+
err = os.Rename(filepath.Join(apath, "config.yml"), newConfigPath)
169+
require.NoError(t, err)
170+
171+
servers := env.RandomizeServerPorts(tmpDir, newConfig)
172+
173+
// Set config homes
174+
env.SetRandomHomeConfig(tmpDir, newConfig)
175+
176+
var (
177+
ctx, cancel = context.WithTimeout(env.Ctx(), serveTimeout)
178+
isBackendAliveErr error
179+
)
180+
go func() {
181+
defer cancel()
182+
isBackendAliveErr = env.IsAppServed(ctx, servers)
183+
}()
184+
env.Must(env.Serve("should serve with Stargate version", apath, "", "", newConfigPath, ExecCtx(ctx)))
149185

150186
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
151187
}

integration/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func testOverwriteSDKConfigsAndChainID(t *testing.T, sdkVersion string) {
3131
env = newEnv(t)
3232
appname = randstr.Runes(10)
3333
path = env.Scaffold(appname, sdkVersion)
34-
servers = env.RandomizeServerPorts(path)
34+
servers = env.RandomizeServerPorts(path, "")
3535
ctx, cancel = context.WithCancel(env.Ctx())
3636
isBackendAliveErr error
3737
)
@@ -51,7 +51,7 @@ func testOverwriteSDKConfigsAndChainID(t *testing.T, sdkVersion string) {
5151
defer cancel()
5252
isBackendAliveErr = env.IsAppServed(ctx, servers)
5353
}()
54-
env.Must(env.Serve("should serve", path, "", "", ExecCtx(ctx)))
54+
env.Must(env.Serve("should serve", path, "", "", "", ExecCtx(ctx)))
5555
require.NoError(t, isBackendAliveErr, "app cannot get online in time")
5656

5757
configs := []struct {

integration/env_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (e env) Scaffold(appName, sdkVersion string) (appPath string) {
182182
// Serve serves an application lives under path with options where msg describes the
183183
// expection from the serving action.
184184
// unless calling with Must(), Serve() will not exit test runtime on failure.
185-
func (e env) Serve(msg, path, home, clihome string, options ...execOption) (ok bool) {
185+
func (e env) Serve(msg, path, home, clihome, configPath string, options ...execOption) (ok bool) {
186186
serveCommand := []string{
187187
"serve",
188188
"-v",
@@ -194,6 +194,9 @@ func (e env) Serve(msg, path, home, clihome string, options ...execOption) (ok b
194194
if clihome != "" {
195195
serveCommand = append(serveCommand, "--cli-home", clihome)
196196
}
197+
if configPath != "" {
198+
serveCommand = append(serveCommand, "--config", configPath)
199+
}
197200

198201
return e.Exec(msg,
199202
step.NewSteps(step.New(
@@ -238,7 +241,11 @@ func (e env) TmpDir() (path string) {
238241

239242
// RandomizeServerPorts randomizes server ports for the app at path, updates
240243
// its config.yml and returns new values.
241-
func (e env) RandomizeServerPorts(path string) starportconf.Servers {
244+
func (e env) RandomizeServerPorts(path string, configFile string) starportconf.Servers {
245+
if configFile == "" {
246+
configFile = "config.yml"
247+
}
248+
242249
// generate random server ports and servers list.
243250
ports, err := availableport.Find(7)
244251
require.NoError(e.t, err)
@@ -258,7 +265,7 @@ func (e env) RandomizeServerPorts(path string) starportconf.Servers {
258265
}
259266

260267
// update config.yml with the generated servers list.
261-
configyml, err := os.OpenFile(filepath.Join(path, "config.yml"), os.O_RDWR|os.O_CREATE, 0755)
268+
configyml, err := os.OpenFile(filepath.Join(path, configFile), os.O_RDWR|os.O_CREATE, 0755)
262269
require.NoError(e.t, err)
263270
defer configyml.Close()
264271

@@ -275,9 +282,13 @@ func (e env) RandomizeServerPorts(path string) starportconf.Servers {
275282
}
276283

277284
// SetRandomHomeConfig sets in the blockchain config files generated temporary directories for home directories
278-
func (e env) SetRandomHomeConfig(path string) {
285+
func (e env) SetRandomHomeConfig(path string, configFile string) {
286+
if configFile == "" {
287+
configFile = "config.yml"
288+
}
289+
279290
// update config.yml with the generated temporary directories
280-
configyml, err := os.OpenFile(filepath.Join(path, "config.yml"), os.O_RDWR|os.O_CREATE, 0755)
291+
configyml, err := os.OpenFile(filepath.Join(path, configFile), os.O_RDWR|os.O_CREATE, 0755)
281292
require.NoError(e.t, err)
282293
defer configyml.Close()
283294

integration/tx_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestGetTxViaGRPCGateway(t *testing.T) {
2323
env = newEnv(t)
2424
appname = randstr.Runes(10)
2525
path = env.Scaffold(appname, Stargate)
26-
servers = env.RandomizeServerPorts(path)
26+
servers = env.RandomizeServerPorts(path, "")
2727
ctx, cancel = context.WithCancel(env.Ctx())
2828
)
2929

@@ -148,7 +148,7 @@ func TestGetTxViaGRPCGateway(t *testing.T) {
148148
isTxBodyRetrieved = env.Exec("retrieve account addresses", steps, ExecRetry())
149149
}()
150150

151-
env.Must(env.Serve("should serve", path, "", "", ExecCtx(ctx)))
151+
env.Must(env.Serve("should serve", path, "", "", "", ExecCtx(ctx)))
152152

153153
if !isTxBodyRetrieved {
154154
t.FailNow()

starport/chainconf/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,14 @@ func (e *ValidationError) Error() string {
186186
return fmt.Sprintf("config is not valid: %s", e.Message)
187187
}
188188

189-
// Locate locates the path for the config file otherwise returns ErrCouldntLocateConfig.
190-
func Locate(root string) (path string, err error) {
189+
// LocateDefault locates the default path for the config file, if no file found returns ErrCouldntLocateConfig.
190+
func LocateDefault(root string) (path string, err error) {
191191
for _, name := range FileNames {
192192
path = filepath.Join(root, name)
193-
if _, err := os.Stat(path); !os.IsNotExist(err) {
193+
if _, err := os.Stat(path); err == nil {
194194
return path, nil
195+
} else if !os.IsNotExist(err) {
196+
return "", err
195197
}
196198
}
197199
return "", ErrCouldntLocateConfig

starport/interface/cli/starport/cmd/serve.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
const flagForceReset = "force-reset"
1010
const flagResetOnce = "reset-once"
11+
const flagConfig = "config"
1112

1213
var appPath string
1314

@@ -23,17 +24,28 @@ func NewServe() *cobra.Command {
2324
c.Flags().StringVarP(&appPath, "path", "p", "", "Path of the app")
2425
c.Flags().BoolP("verbose", "v", false, "Verbose output")
2526
c.Flags().BoolP(flagForceReset, "f", false, "Force reset of the app state on start and every source change")
26-
c.Flags().BoolP(flagResetOnce, "r", false, "Reset of the app state on first start")
27+
c.Flags().BoolP(flagResetOnce, "r", false, "Reset of -the app state on first start")
28+
c.Flags().StringP(flagConfig, "c", "", "Starport config file (default: ./config.yml)")
2729

2830
return c
2931
}
3032

3133
func serveHandler(cmd *cobra.Command, args []string) error {
32-
// create the chain
3334
chainOption := []chain.Option{
3435
chain.LogLevel(logLevel(cmd)),
3536
chain.KeyringBackend(chaincmd.KeyringBackendTest),
3637
}
38+
39+
// check if custom config is defined
40+
config, err := cmd.Flags().GetString(flagConfig)
41+
if err != nil {
42+
return err
43+
}
44+
if config != "" {
45+
chainOption = append(chainOption, chain.ConfigFile(config))
46+
}
47+
48+
// create the chain
3749
c, err := newChainWithHomeFlags(cmd, appPath, chainOption...)
3850
if err != nil {
3951
return err

starport/pkg/dirchange/dirchange.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func checksumFromPaths(workdir string, paths []string) ([]byte, error) {
7979

8080
// read files
8181
for _, path := range paths {
82-
if workdir != "" {
82+
if !filepath.IsAbs(path) {
8383
path = filepath.Join(workdir, path)
8484
}
8585

0 commit comments

Comments
 (0)