Skip to content

Commit

Permalink
/task/new implemented, progress bar, streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Jan 10, 2019
1 parent fb9a8f9 commit 304359d
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 72 deletions.
82 changes: 82 additions & 0 deletions internal/cmd/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright © 2018 CloudODM Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"fmt"

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

var argsCmd = &cobra.Command{
Use: "args",
Aliases: []string{"arguments"},
Short: "View arguments",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()

config.CheckLogin(nodeName, "", "")

node, err := config.User.GetNode(nodeName)
if err != nil {
logger.Error(err)
}

options, err := node.Options()
if err != nil {
logger.Error(err)
}

logger.Info("Args:")
logger.Info("")

for _, option := range options {
domain := ""
switch option.Domain.(type) {
case string:
domain = option.Domain.(string)
case []interface{}:
for i, v := range option.Domain.([]interface{}) {
if i > 0 {
domain += ","
}
if v == "" {
v = "\"\""
}
domain += fmt.Sprint(v)
}
default:
domain = "?"
}

if domain != "" {
domain = "<" + domain + ">"
}

logger.Info("--" + option.Name + " " + domain)
logger.Info(option.Help)
logger.Info("")
}
},
}

func init() {
argsCmd.Flags().StringVarP(&nodeName, "node", "n", "default", "Processing node to use")

rootCmd.AddCommand(argsCmd)
}
7 changes: 6 additions & 1 deletion internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ import (
"github.com/spf13/cobra"
)

var username string
var password string

var loginCmd = &cobra.Command{
Use: "login [--node default]",
Short: "Login with a node",
Run: func(cmd *cobra.Command, args []string) {
config.Initialize()

if config.CheckLogin(nodeName) != nil {
if config.CheckLogin(nodeName, username, password) != nil {
logger.Info("Logged in")
}
},
}

func init() {
loginCmd.Flags().StringVarP(&nodeName, "node", "n", "default", "Processing node to use")
loginCmd.Flags().StringVar(&username, "username", "", "Username")
loginCmd.Flags().StringVar(&password, "password", "", "Password")

rootCmd.AddCommand(loginCmd)
}
71 changes: 69 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/OpenDroneMap/CloudODM/internal/config"
"github.com/OpenDroneMap/CloudODM/internal/fs"
"github.com/OpenDroneMap/CloudODM/internal/logger"
"github.com/OpenDroneMap/CloudODM/internal/odm"

"github.com/spf13/cobra"
)
Expand All @@ -34,7 +35,7 @@ var outputPath string
var nodeName string

var rootCmd = &cobra.Command{
Use: "odm [flags] <images> [<gcp>] [parameters]",
Use: "odm [flags] <images> [<gcp>] [args]",
Short: "A command line tool to process aerial imagery in the cloud",

Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -54,14 +55,26 @@ var rootCmd = &cobra.Command{

logger.Debug("Options: " + strings.Join(options, " "))

info := config.CheckLogin(nodeName)
info := config.CheckLogin(nodeName, "", "")

// Check max images
if len(inputFiles) > info.MaxImages {
logger.Error("Cannot process", len(inputFiles), "files with this node, the node has a limit of", info.MaxImages)
}

logger.Debug("NodeODM version: " + info.Version)

node, err := config.User.GetNode(nodeName)
if err != nil {
logger.Error(err)
}

nodeOptions, err := node.Options()
if err != nil {
logger.Error(err)
}

odm.Run(inputFiles, parseOptions(options, nodeOptions), *node)
},

TraverseChildren: true,
Expand Down Expand Up @@ -127,3 +140,57 @@ func filterImagesAndText(files []string) []string {

return result
}

func invalidArg(arg string) {
logger.Error("Invalid argument " + arg + ". See ./odm args for a list of valid arguments.")
}

func parseOptions(options []string, nodeOptions []odm.OptionResponse) []odm.Option {
result := []odm.Option{}

for i := 0; i < len(options); i++ {
o := options[i]

if strings.HasPrefix(o, "--") || strings.HasPrefix(o, "-") {
currentOption := odm.Option{}

// Key
o = strings.TrimPrefix(o, "--")
o = strings.TrimPrefix(o, "-")

found := false
optType := "string"
for _, no := range nodeOptions {
if no.Name == o {
found = true
optType = no.Type
break
}
}

if !found {
invalidArg(o)
}

// TODO: domain checks

currentOption.Name = o
if optType == "bool" {
currentOption.Value = "true"
} else {
if i < len(options)-1 {
currentOption.Value = options[i+1]
i++
} else {
invalidArg(o)
}
}

result = append(result, currentOption)
} else {
invalidArg(o)
}
}

return result
}
44 changes: 0 additions & 44 deletions internal/cmd/run.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// if it does, it attempts to login
// it it doesn't, returns node.Info()
// on error, it prints a message and exits
func CheckLogin(nodeName string) *odm.InfoResponse {
func CheckLogin(nodeName string, username string, password string) *odm.InfoResponse {
node, err := User.GetNode(nodeName)
if err != nil {
logger.Error(err)
Expand All @@ -19,7 +19,7 @@ func CheckLogin(nodeName string) *odm.InfoResponse {
err = node.CheckAuthentication(err)
if err != nil {
if err == odm.ErrAuthRequired {
token, err := node.TryLogin()
token, err := node.TryLogin(username, password)
if err != nil {
logger.Error(err)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (c Configuration) Save() {
type Configuration struct {
Nodes map[string]odm.Node `json:"nodes"`

Tasks []odm.Task `json:"tasks"`

filePath string
}

Expand Down
30 changes: 30 additions & 0 deletions internal/io/terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io

import (
"bufio"
"fmt"
"os"
"strings"
"syscall"

"golang.org/x/crypto/ssh/terminal"
)

func GetUsernamePassword() (username string, password string) {
reader := bufio.NewReader(os.Stdin)
username = ""
for len(username) == 0 {
fmt.Print("Enter username: ")
username, _ = reader.ReadString('\n')
username = strings.TrimSpace(username)
}

password = ""
for len(password) == 0 {
fmt.Print("Enter password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
password = string(bytePassword)
}

return username, password
}
Loading

0 comments on commit 304359d

Please sign in to comment.