Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #114 from Zandrr/help-command-pr
Browse files Browse the repository at this point in the history
added architecture for usage via -help
  • Loading branch information
Luis Pabón committed Aug 6, 2015
2 parents 25d18bf + 2610c69 commit ce40304
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 18 deletions.
7 changes: 6 additions & 1 deletion client/go/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package commands
import (
"errors"
"flag"
"fmt"
"github.com/lpabon/godbc"
)

Expand All @@ -33,7 +34,6 @@ func NewClusterCommand(options *Options) *ClusterCommand {

//require before we do any work
godbc.Require(options != nil)
godbc.Require(options.Url != "")

//create ClusterCommand object
cmd := &ClusterCommand{}
Expand All @@ -51,6 +51,11 @@ func NewClusterCommand(options *Options) *ClusterCommand {
//create flags
cmd.flags = flag.NewFlagSet(cmd.name, flag.ExitOnError)

//usage on -help
cmd.flags.Usage = func() {
fmt.Println(usageTemplateCluster)
}

//ensure before we return
godbc.Ensure(cmd.flags != nil)
godbc.Ensure(cmd.name == "cluster")
Expand Down
13 changes: 12 additions & 1 deletion client/go/commands/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/heketi/heketi/utils"
"github.com/lpabon/godbc"
"net/http"
"os"
)

type CreateNewClusterCommand struct {
Expand All @@ -35,13 +36,17 @@ type CreateNewClusterCommand struct {
func NewCreateNewClusterCommand(options *Options) *CreateNewClusterCommand {

godbc.Require(options != nil)
godbc.Require(options.Url != "")

cmd := &CreateNewClusterCommand{}
cmd.name = "create"
cmd.options = options
cmd.flags = flag.NewFlagSet(cmd.name, flag.ExitOnError)

//usage on -help
cmd.flags.Usage = func() {
fmt.Println(usageTemplateClusterCreate)
}

godbc.Ensure(cmd.flags != nil)
godbc.Ensure(cmd.name == "create")

Expand All @@ -58,6 +63,12 @@ func (a *CreateNewClusterCommand) Exec(args []string) error {
//parse args
a.flags.Parse(args)

//ensure we have Url
if a.options.Url == "" {
fmt.Fprintf(stdout, "You need a server!\n")
os.Exit(1)
}

s := a.flags.Args()
//ensure length
if len(s) > 0 {
Expand Down
13 changes: 12 additions & 1 deletion client/go/commands/cluster_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/heketi/heketi/utils"
"github.com/lpabon/godbc"
"net/http"
"os"
)

type DestroyClusterCommand struct {
Expand All @@ -33,13 +34,17 @@ type DestroyClusterCommand struct {
func NewDestroyClusterCommand(options *Options) *DestroyClusterCommand {

godbc.Require(options != nil)
godbc.Require(options.Url != "")

cmd := &DestroyClusterCommand{}
cmd.name = "destroy"
cmd.options = options
cmd.flags = flag.NewFlagSet(cmd.name, flag.ExitOnError)

//usage on -help
cmd.flags.Usage = func() {
fmt.Println(usageTemplateClusterDestroy)
}

godbc.Ensure(cmd.flags != nil)
godbc.Ensure(cmd.name == "destroy")

Expand All @@ -56,6 +61,12 @@ func (a *DestroyClusterCommand) Exec(args []string) error {
//parse args
a.flags.Parse(args)

//ensure we have Url
if a.options.Url == "" {
fmt.Fprintf(stdout, "You need a server!\n")
os.Exit(1)
}

s := a.flags.Args()

//ensure proper number of args
Expand Down
13 changes: 12 additions & 1 deletion client/go/commands/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/heketi/heketi/utils"
"github.com/lpabon/godbc"
"net/http"
"os"
)

type GetClusterInfoCommand struct {
Expand All @@ -34,13 +35,17 @@ type GetClusterInfoCommand struct {
func NewGetClusterInfoCommand(options *Options) *GetClusterInfoCommand {

godbc.Require(options != nil)
godbc.Require(options.Url != "")

cmd := &GetClusterInfoCommand{}
cmd.name = "info"
cmd.options = options
cmd.flags = flag.NewFlagSet(cmd.name, flag.ExitOnError)

//usage on -help
cmd.flags.Usage = func() {
fmt.Println(usageTemplateClusterInfo)
}

godbc.Ensure(cmd.flags != nil)
godbc.Ensure(cmd.name == "info")

Expand All @@ -56,6 +61,12 @@ func (a *GetClusterInfoCommand) Exec(args []string) error {
//parse flags and set id
a.flags.Parse(args)

//ensure we have Url
if a.options.Url == "" {
fmt.Fprintf(stdout, "You need a server!\n")
os.Exit(1)
}

s := a.flags.Args()

//ensure correct number of args
Expand Down
13 changes: 12 additions & 1 deletion client/go/commands/cluster_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/heketi/heketi/utils"
"github.com/lpabon/godbc"
"net/http"
"os"
)

type GetClusterListCommand struct {
Expand All @@ -34,13 +35,17 @@ type GetClusterListCommand struct {
func NewGetClusterListCommand(options *Options) *GetClusterListCommand {

godbc.Require(options != nil)
godbc.Require(options.Url != "")

cmd := &GetClusterListCommand{}
cmd.name = "list"
cmd.options = options
cmd.flags = flag.NewFlagSet(cmd.name, flag.ExitOnError)

//usage on -help
cmd.flags.Usage = func() {
fmt.Println(usageTemplateClusterList)
}

godbc.Ensure(cmd.flags != nil)
godbc.Ensure(cmd.name == "list")

Expand All @@ -57,6 +62,12 @@ func (a *GetClusterListCommand) Exec(args []string) error {
//parse args
a.flags.Parse(args)

//ensure we have Url
if a.options.Url == "" {
fmt.Fprintf(stdout, "You need a server!\n")
os.Exit(1)
}

s := a.flags.Args()

//ensure number of args
Expand Down
94 changes: 94 additions & 0 deletions client/go/commands/usage_templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// Copyright (c) 2015 The heketi Authors
//
// 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 commands

var usageTemplateCluster = `Cluster is a command used for managing heketi clusters.
Usage:
heketi -server [server] [options] cluster [subcommands]
The subcommands are:
create Creates a new cluster for Heketi to manage.
list Returns a list of all clusters on the specified server.
info [id] Returns information about a specific cluster.
destroy [id] Destroys cluster with specified id.
Use "heketi cluster [subcommand] -help" for more information about a subcommand
`

var usageTemplateClusterDestroy = `cluster destroy is a command used for destroying heketi clusters.
Usage:
heketi -server [server] [options] cluster destroy [args]
The args are:
cluster id The id of the cluster you want to destroy
Example:
heketi -server http://localhost:8080 cluster destroy 0854b5f5405cac5280c7dc479cd0e7fb
`

var usageTemplateClusterCreate = `cluster create is a command used for creating heketi clusters.
Usage:
heketi -server [server] [options] cluster create
This command takes no arguments.
Example:
heketi -server http://localhost:8080 cluster create
`

var usageTemplateClusterInfo = `cluster info is a command used for getting more information about a cluster.
Usage:
heketi -server [server] [options] cluster info [args]
The args are:
cluster id The id of the cluster you want to destroy
Example:
heketi -server http://localhost:8080 cluster info 0854b5f5405cac5280c7dc479cd0e7fb
`

var usageTemplateClusterList = `cluster list is a command used for getting a list of clusters on the specified server.
Usage:
heketi -server [server] [options] cluster list
This command takes no arguments.
Example:
heketi -server http://localhost:8080 cluster list
`
36 changes: 23 additions & 13 deletions client/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,28 @@ import (
)

var (
stdout io.Writer = os.Stdout
options commands.Options
stdout io.Writer = os.Stdout
options commands.Options
usageTemplateMain = `Heketi is a tool for managing gluster volumes.
Usage:
heketi -server [server] [options] command [arguments]
Options are:
-json Returns any command in JSON format.
The commands are:
cluster Manage a cluster (a set of storage nodes)
node Register a storage system to be managed
device Manage raw devices in a cluster
volume Manage a network file system of a certain size available to be used by clients.
Use "heketi [command] -help" for more information about a command
`
)

func init() {
Expand All @@ -35,24 +55,14 @@ func init() {
flag.BoolVar(&options.Json, "json", false, "get response as json")

flag.Usage = func() {
fmt.Println("USAGE: ")
fmt.Println("heketi cluster <n>")
fmt.Println("where n can be one of the following: ")
fmt.Println("create <id> \n info <id> \n list \n destroy <id>")

//TODO: add other first level commands
fmt.Println(usageTemplateMain)
}
}

// ------ Main
func main() {
flag.Parse()

//ensure that we pass a server
if options.Url == "" {
fmt.Fprintf(stdout, "You need a server!\n")
os.Exit(1)
}
//all first level commands go here (cluster, node, device, volume)
cmds := commands.Commands{
commands.NewClusterCommand(&options),
Expand Down

0 comments on commit ce40304

Please sign in to comment.