From 6f3ab8485211aa0d332961f76373bcee5cc45026 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Tue, 18 May 2021 16:00:22 +0200 Subject: [PATCH 1/5] todo refactor --- pkg/flowcli/config/json/account.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/flowcli/config/json/account.go b/pkg/flowcli/config/json/account.go index 02b66205c..dda32c33c 100644 --- a/pkg/flowcli/config/json/account.go +++ b/pkg/flowcli/config/json/account.go @@ -139,6 +139,7 @@ func transformAccountsToJSON(accounts config.Accounts) jsonAccounts { return jsonAccounts } +// TODO(sideninja) refactor keys to use json.RawMessage and decide if unmarshal into simple or advance structure based on that field type jsonAccountSimple struct { Address string `json:"address"` Keys string `json:"keys"` From 78b1ba255b9bd02f4bb44cb449e6be7410e4d0d7 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 18:23:59 +0200 Subject: [PATCH 2/5] enable config command and remove project init --- internal/config/config.go | 2 +- internal/project/init.go | 60 ------------------------------------- internal/project/project.go | 1 - 3 files changed, 1 insertion(+), 62 deletions(-) delete mode 100644 internal/project/init.go diff --git a/internal/config/config.go b/internal/config/config.go index 6f6a52ef9..7f29371ff 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -29,7 +29,7 @@ var Cmd = &cobra.Command{ } func init() { - //InitCommand.AddToParent(Cmd) + InitCommand.AddToParent(Cmd) Cmd.AddCommand(AddCmd) Cmd.AddCommand(RemoveCmd) } diff --git a/internal/project/init.go b/internal/project/init.go deleted file mode 100644 index 33d6d710a..000000000 --- a/internal/project/init.go +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Flow CLI - * - * Copyright 2019-2021 Dapper Labs, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package project - -import ( - "fmt" - - "github.com/spf13/cobra" - - "github.com/onflow/flow-cli/internal/command" - "github.com/onflow/flow-cli/internal/config" - "github.com/onflow/flow-cli/pkg/flowcli/services" -) - -var initFlag = config.FlagsInit{} - -var InitCommand = &command.Command{ - Cmd: &cobra.Command{ - Use: "init", - Short: "Initialize a new configuration", - Example: "flow project init", - }, - Flags: &initFlag, - Run: func( - cmd *cobra.Command, - args []string, - globalFlags command.GlobalFlags, - services *services.Services, - ) (command.Result, error) { - fmt.Println("⚠️ DEPRECATION WARNING: use \"flow init\" instead") - - proj, err := services.Project.Init( - initFlag.Reset, - initFlag.ServiceKeySigAlgo, - initFlag.ServiceKeyHashAlgo, - initFlag.ServicePrivateKey, - ) - if err != nil { - return nil, err - } - - return &config.InitResult{Project: proj}, nil - }, -} diff --git a/internal/project/project.go b/internal/project/project.go index 1d766474f..ed25a8404 100644 --- a/internal/project/project.go +++ b/internal/project/project.go @@ -29,7 +29,6 @@ var Cmd = &cobra.Command{ } func init() { - InitCommand.AddToParent(Cmd) DeployCommand.AddToParent(Cmd) Cmd.AddCommand(EmulatorCommand) } From 37f6da9fdacc3b623d566b4985487397baf7dd3b Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 18:46:13 +0200 Subject: [PATCH 3/5] shortctus workaround --- internal/shortcuts/init.go | 102 +++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 internal/shortcuts/init.go diff --git a/internal/shortcuts/init.go b/internal/shortcuts/init.go new file mode 100644 index 000000000..d48d38a93 --- /dev/null +++ b/internal/shortcuts/init.go @@ -0,0 +1,102 @@ +/* + * Flow CLI + * + * Copyright 2019-2021 Dapper Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package shortcuts + +import ( + "bytes" + "fmt" + "text/tabwriter" + + "github.com/spf13/cobra" + + "github.com/onflow/flow-cli/internal/command" + "github.com/onflow/flow-cli/pkg/flowcli/project" + "github.com/onflow/flow-cli/pkg/flowcli/services" + "github.com/onflow/flow-cli/pkg/flowcli/util" +) + +// TODO(sideninja) workaround - init needed to be copied in order to work else there is flag duplicate error + +type FlagsInit struct { + ServicePrivateKey string `flag:"service-private-key" info:"Service account private key"` + ServiceKeySigAlgo string `default:"ECDSA_P256" flag:"service-sig-algo" info:"Service account key signature algorithm"` + ServiceKeyHashAlgo string `default:"SHA3_256" flag:"service-hash-algo" info:"Service account key hash algorithm"` + Reset bool `default:"false" flag:"reset" info:"Reset flow.json config file"` +} + +var initFlag = FlagsInit{} + +var InitHotCommand = &command.Command{ + Cmd: &cobra.Command{ + Use: "init", + Short: "Initialize a new configuration", + }, + Flags: &initFlag, + Run: func( + cmd *cobra.Command, + args []string, + globalFlags command.GlobalFlags, + services *services.Services, + ) (command.Result, error) { + proj, err := services.Project.Init( + initFlag.Reset, + initFlag.ServiceKeySigAlgo, + initFlag.ServiceKeyHashAlgo, + initFlag.ServicePrivateKey, + ) + if err != nil { + return nil, err + } + + return &InitResult{proj}, nil + }, +} + +// InitResult result structure +type InitResult struct { + *project.Project +} + +// JSON convert result to JSON +func (r *InitResult) JSON() interface{} { + return r +} + +// String convert result to string +func (r *InitResult) String() string { + var b bytes.Buffer + writer := tabwriter.NewWriter(&b, 0, 8, 1, '\t', tabwriter.AlignRight) + account, _ := r.Project.EmulatorServiceAccount() + + fmt.Fprintf(writer, "Configuration initialized\n") + fmt.Fprintf(writer, "Service account: %s\n\n", util.Bold("0x"+account.Address().String())) + fmt.Fprintf(writer, + "Start emulator by running: %s \nReset configuration using: %s\n", + util.Bold("'flow emulator'"), + util.Bold("'flow init --reset'"), + ) + + writer.Flush() + return b.String() +} + +// Oneliner show result as one liner grep friendly +func (r *InitResult) Oneliner() string { + return "" +} From 110cbb3f335ae380a1672e0b561f41db583c6bee Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 18:50:55 +0200 Subject: [PATCH 4/5] enable quick commands --- cmd/flow/main.go | 5 +++-- internal/{shortcuts => quick}/init.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) rename internal/{shortcuts => quick}/init.go (99%) diff --git a/cmd/flow/main.go b/cmd/flow/main.go index 22a0dc458..973c6242b 100644 --- a/cmd/flow/main.go +++ b/cmd/flow/main.go @@ -20,6 +20,7 @@ package main import ( + "github.com/onflow/flow-cli/internal/quick" "github.com/spf13/cobra" "github.com/onflow/flow-cli/internal/accounts" @@ -44,8 +45,8 @@ func main() { TraverseChildren: true, } - // hot commands - config.InitCommand.AddToParent(cmd) + // quick commands + quick.InitHotCommand.AddToParent(cmd) // structured commands cmd.AddCommand(cadence.Cmd) diff --git a/internal/shortcuts/init.go b/internal/quick/init.go similarity index 99% rename from internal/shortcuts/init.go rename to internal/quick/init.go index d48d38a93..195bd1697 100644 --- a/internal/shortcuts/init.go +++ b/internal/quick/init.go @@ -16,7 +16,7 @@ * limitations under the License. */ -package shortcuts +package quick import ( "bytes" From 28d225b71d27a2431487ce62212ebfed8c1bb7e9 Mon Sep 17 00:00:00 2001 From: Gregor Gololicic Date: Thu, 20 May 2021 19:47:33 +0200 Subject: [PATCH 5/5] command fix import --- cmd/flow/main.go | 6 ++-- internal/project/init.go | 0 internal/quick/init.go | 59 +++++++--------------------------------- 3 files changed, 13 insertions(+), 52 deletions(-) delete mode 100644 internal/project/init.go diff --git a/cmd/flow/main.go b/cmd/flow/main.go index ff19f5f5d..e6c803707 100644 --- a/cmd/flow/main.go +++ b/cmd/flow/main.go @@ -20,7 +20,6 @@ package main import ( - "github.com/onflow/flow-cli/internal/quick" "github.com/spf13/cobra" "github.com/onflow/flow-cli/internal/accounts" @@ -33,6 +32,7 @@ import ( "github.com/onflow/flow-cli/internal/events" "github.com/onflow/flow-cli/internal/keys" "github.com/onflow/flow-cli/internal/project" + "github.com/onflow/flow-cli/internal/quick" "github.com/onflow/flow-cli/internal/scripts" "github.com/onflow/flow-cli/internal/status" "github.com/onflow/flow-cli/internal/transactions" @@ -47,7 +47,8 @@ func main() { } // quick commands - quick.InitHotCommand.AddToParent(cmd) + quick.InitCommand.AddToParent(cmd) + status.Command.AddToParent(cmd) // structured commands cmd.AddCommand(cadence.Cmd) @@ -59,7 +60,6 @@ func main() { cmd.AddCommand(keys.Cmd) cmd.AddCommand(events.Cmd) cmd.AddCommand(blocks.Cmd) - cmd.AddCommand(status.Cmd) cmd.AddCommand(collections.Cmd) cmd.AddCommand(project.Cmd) cmd.AddCommand(config.Cmd) diff --git a/internal/project/init.go b/internal/project/init.go deleted file mode 100644 index e69de29bb..000000000 diff --git a/internal/quick/init.go b/internal/quick/init.go index 195bd1697..19b4fb90b 100644 --- a/internal/quick/init.go +++ b/internal/quick/init.go @@ -19,33 +19,24 @@ package quick import ( - "bytes" "fmt" - "text/tabwriter" "github.com/spf13/cobra" "github.com/onflow/flow-cli/internal/command" - "github.com/onflow/flow-cli/pkg/flowcli/project" + "github.com/onflow/flow-cli/internal/config" "github.com/onflow/flow-cli/pkg/flowcli/services" - "github.com/onflow/flow-cli/pkg/flowcli/util" ) // TODO(sideninja) workaround - init needed to be copied in order to work else there is flag duplicate error -type FlagsInit struct { - ServicePrivateKey string `flag:"service-private-key" info:"Service account private key"` - ServiceKeySigAlgo string `default:"ECDSA_P256" flag:"service-sig-algo" info:"Service account key signature algorithm"` - ServiceKeyHashAlgo string `default:"SHA3_256" flag:"service-hash-algo" info:"Service account key hash algorithm"` - Reset bool `default:"false" flag:"reset" info:"Reset flow.json config file"` -} - -var initFlag = FlagsInit{} +var initFlag = config.FlagsInit{} -var InitHotCommand = &command.Command{ +var InitCommand = &command.Command{ Cmd: &cobra.Command{ - Use: "init", - Short: "Initialize a new configuration", + Use: "init", + Short: "Initialize a new configuration", + Example: "flow project init", }, Flags: &initFlag, Run: func( @@ -54,8 +45,11 @@ var InitHotCommand = &command.Command{ globalFlags command.GlobalFlags, services *services.Services, ) (command.Result, error) { + fmt.Println("⚠️ DEPRECATION WARNING: use \"flow init\" instead") + proj, err := services.Project.Init( initFlag.Reset, + initFlag.Global, initFlag.ServiceKeySigAlgo, initFlag.ServiceKeyHashAlgo, initFlag.ServicePrivateKey, @@ -64,39 +58,6 @@ var InitHotCommand = &command.Command{ return nil, err } - return &InitResult{proj}, nil + return &config.InitResult{Project: proj}, nil }, } - -// InitResult result structure -type InitResult struct { - *project.Project -} - -// JSON convert result to JSON -func (r *InitResult) JSON() interface{} { - return r -} - -// String convert result to string -func (r *InitResult) String() string { - var b bytes.Buffer - writer := tabwriter.NewWriter(&b, 0, 8, 1, '\t', tabwriter.AlignRight) - account, _ := r.Project.EmulatorServiceAccount() - - fmt.Fprintf(writer, "Configuration initialized\n") - fmt.Fprintf(writer, "Service account: %s\n\n", util.Bold("0x"+account.Address().String())) - fmt.Fprintf(writer, - "Start emulator by running: %s \nReset configuration using: %s\n", - util.Bold("'flow emulator'"), - util.Bold("'flow init --reset'"), - ) - - writer.Flush() - return b.String() -} - -// Oneliner show result as one liner grep friendly -func (r *InitResult) Oneliner() string { - return "" -}