From dcf7a8f52bd05612e072211990577498cd13adb3 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Wed, 17 Jul 2019 12:36:58 +0200 Subject: [PATCH] style: create main.go with flags --- cmd/gpm/main.go | 3 +-- gpm/cli.go | 36 ------------------------------- gpm/main.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 gpm/main.go diff --git a/cmd/gpm/main.go b/cmd/gpm/main.go index 23b498a..e2673cf 100644 --- a/cmd/gpm/main.go +++ b/cmd/gpm/main.go @@ -19,6 +19,5 @@ import( ) func main() { - c := gpm.Cli{} - c.Run() + gpm.Run() } diff --git a/gpm/cli.go b/gpm/cli.go index 2533b1e..062dce3 100644 --- a/gpm/cli.go +++ b/gpm/cli.go @@ -16,7 +16,6 @@ package gpm import( "bufio" - "flag" "fmt" "os" "strconv" @@ -28,20 +27,6 @@ import( "golang.org/x/crypto/ssh/terminal" ) -// Options -var( - ADD = flag.Bool("add", false, "add a new entry in the wallet") - UPDATE = flag.Bool("update", false, "update an entry") - DELETE = flag.Bool("delete", false, "delete an entry") - LIST = flag.Bool("list", false, "list the entries in a wallet") - COPY = flag.Bool("copy", false, "enter an copy mode for an entry") - CONFIG = flag.String("config", "", "specify the config file") - GROUP = flag.String("group", "", "search the entries in this group ") - PATTERN = flag.String("pattern", "", "search the entries with this pattern") - WALLET = flag.String("wallet", "", "specify the wallet") - HELP = flag.Bool("help", false, "print this help message") -) - // Cli contain config and wallet to use type Cli struct { Config Config @@ -261,24 +246,3 @@ func (c *Cli) copyEntry() { } } } - -// Run the cli interface -func (c *Cli) Run() { - c.Config.Load(*CONFIG) - - flag.Parse() - if *HELP { - flag.PrintDefaults() - os.Exit(1) - } else if *LIST { - c.listEntry() - } else if *COPY { - c.copyEntry() - } else if *ADD { - c.addEntry() - } else if *UPDATE { - c.updateEntry() - } else if *DELETE { - c.deleteEntry() - } -} diff --git a/gpm/main.go b/gpm/main.go new file mode 100644 index 0000000..cf02515 --- /dev/null +++ b/gpm/main.go @@ -0,0 +1,56 @@ +// Copyright 2019 Adrien Waksberg +// +// 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 +// +// https://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 gpm + +import( + "flag" + "os" +) + +// Options +var( + ADD = flag.Bool("add", false, "add a new entry in the wallet") + UPDATE = flag.Bool("update", false, "update an entry") + DELETE = flag.Bool("delete", false, "delete an entry") + LIST = flag.Bool("list", false, "list the entries in a wallet") + COPY = flag.Bool("copy", false, "enter an copy mode for an entry") + CONFIG = flag.String("config", "", "specify the config file") + GROUP = flag.String("group", "", "search the entries in this group ") + PATTERN = flag.String("pattern", "", "search the entries with this pattern") + WALLET = flag.String("wallet", "", "specify the wallet") + HELP = flag.Bool("help", false, "print this help message") +) + +// Run the cli interface +func Run() { + var cli Cli + cli.Config.Load(*CONFIG) + + flag.Parse() + if *HELP { + flag.PrintDefaults() + os.Exit(1) + } else if *LIST { + cli.listEntry() + } else if *COPY { + cli.copyEntry() + } else if *ADD { + cli.addEntry() + } else if *UPDATE { + cli.updateEntry() + } else if *DELETE { + cli.deleteEntry() + } +}