Skip to content

Commit

Permalink
Add configure command
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Aug 13, 2020
1 parent 6b39266 commit 9aaca48
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
47 changes: 47 additions & 0 deletions cmd/reva/configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2018-2020 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"bufio"
"fmt"
"io"
"os"
)

var configureCommand = func() *command {
cmd := newCommand("configure")
cmd.Description = func() string { return "configure the reva client" }
cmd.Action = func(w ...io.Writer) error {
reader := bufio.NewReader(os.Stdin)
fmt.Print("host: ")
text, err := read(reader)
if err != nil {
return err
}

c := &config{Host: text}
if err := writeConfig(c); err != nil {
return err
}
fmt.Println("config saved at ", getConfigFile())
return nil
}
return cmd
}
11 changes: 11 additions & 0 deletions cmd/reva/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func (e *Executor) Execute(s string) {

args := strings.Split(s, " ")

// Verify that the configuration is set, either in memory or in a file.
if conf.Host == "" {
c, err := readConfig()
if err != nil && args[0] != "configure" {
fmt.Println("reva is not configured, please run the configure command")
return
} else if args[0] != "configure" {
conf = c
}
}

action := args[0]
for _, v := range commands {
if v.Name == action {
Expand Down
10 changes: 2 additions & 8 deletions cmd/reva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (

commands = []*command{
versionCommand(),
configureCommand(),
loginCommand(),
whoamiCommand(),
importCommand(),
Expand Down Expand Up @@ -82,14 +83,7 @@ func init() {

func main() {

if host == "" {
c, err := readConfig()
if err != nil {
fmt.Println("reva is not configured, please pass the \"-host\" flag")
os.Exit(1)
}
conf = c
} else {
if host != "" {
conf = &config{host}
if err := writeConfig(conf); err != nil {
fmt.Println("error writing to config file")
Expand Down

0 comments on commit 9aaca48

Please sign in to comment.