Skip to content

Commit

Permalink
Added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Dec 20, 2018
1 parent 06d8d11 commit a9e1a3f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 14 deletions.
10 changes: 7 additions & 3 deletions cmd/odm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

package main

import "github.com/OpenDroneMap/CloudODM/internal/cmd"
import "github.com/OpenDroneMap/CloudODM/internal/config"
import (
"github.com/OpenDroneMap/CloudODM/internal/cmd"
)

func main() {
config.Initialize("odm")
// fmt.Print("Enter password: ")
// bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
// password := string(bytePassword)
// fmt.Printf("Your password is '%s'", password)
cmd.Execute()
}
4 changes: 3 additions & 1 deletion internal/cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd
import (
"fmt"

"github.com/OpenDroneMap/CloudODM/internal/config"
"github.com/spf13/cobra"
)

Expand All @@ -26,8 +27,9 @@ var nodeCmd = &cobra.Command{
Use: "node",
Short: "Manage processing nodes",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()

fmt.Println("node called")
fmt.Println(Verbose)
},
}

Expand Down
13 changes: 7 additions & 6 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ package cmd

import (
"fmt"
"os"

"github.com/OpenDroneMap/CloudODM/internal/config"
"github.com/OpenDroneMap/CloudODM/internal/logger"
"github.com/spf13/cobra"
)

var Verbose bool

var rootCmd = &cobra.Command{
Use: "odm",
Short: "A command line tool to process aerial imagery in the cloud",

Run: func(cmd *cobra.Command, args []string) {
config.Initialize()

fmt.Printf("TODO: %v\n", args)

},

TraverseChildren: true,
Expand All @@ -41,11 +43,10 @@ var rootCmd = &cobra.Command{

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
logger.Error(err)
}
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "show verbose output")
rootCmd.PersistentFlags().BoolVarP(&logger.Verbose, "verbose", "v", false, "show verbose output")
}
3 changes: 3 additions & 0 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import (
"fmt"
"os"

"github.com/OpenDroneMap/CloudODM/internal/config"
"github.com/spf13/cobra"
)

var runCmd = &cobra.Command{
Use: "run",
Short: "Process a dataset",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()

fmt.Println("run called")
fmt.Println(os.Args[2:])
fmt.Println(args)
Expand Down
4 changes: 2 additions & 2 deletions internal/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ type Node struct {
}

// Initialize the configuration
func Initialize(name string) {
func Initialize() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

cfgPath := filepath.Join(home, "."+name+".json")
cfgPath := filepath.Join(home, ".odm.json")
GetPublicNodes()
fmt.Println(cfgPath)
}
15 changes: 13 additions & 2 deletions internal/config/publicnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"

"github.com/OpenDroneMap/CloudODM/internal/logger"
)

type PublicNode struct {
Expand All @@ -14,19 +17,27 @@ type PublicNode struct {
}

func GetPublicNodes() []PublicNode {
logger.Debug("Retrieving public nodes...")

resp, err := http.Get("https://github.com/raw/OpenDroneMap/CloudODM/master/public_nodes.json")
if err != nil {
logger.Info(err)
return nil
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Info(err)
return nil
}

nodes := []PublicNode{}
json.Unmarshal(body, &nodes)
if err := json.Unmarshal(body, &nodes); err != nil {
logger.Info("Invalid JSON content: " + string(body))
return nil
}

return nil
logger.Info("Loaded " + strconv.Itoa(len(nodes)) + " public nodes")
return nodes
}
27 changes: 27 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package logger

import (
"fmt"
"os"
)

// Verbose output
var Verbose bool

// Debug message (if verbose is enabled)
func Debug(a ...interface{}) {
if Verbose {
fmt.Println(a...)
}
}

// Info message
func Info(a ...interface{}) {
fmt.Println(a...)
}

//Error message and exit
func Error(a ...interface{}) {
fmt.Println(a...)
os.Exit(1)
}

0 comments on commit a9e1a3f

Please sign in to comment.