Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to download the calculation by id or value #2

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions cmd/ccboc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/user"
"path/filepath"
"sort"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -121,6 +122,83 @@ func getCalculationID(c *cli.Context) error {
return nil
}

func getCalculationResult(c *cli.Context) error {
teff := c.Float64("teff")
logG := c.Float64("logG")
path := c.String("results-download-path")

resp, err := http.Get(globalConfig.APIURL + "/calculations/results?teff=" + fmt.Sprintf("%0.1f", teff) + "&logg=" + fmt.Sprintf("%0.2f", logG))
if err != nil {
logrus.WithError(err).Fatal("Couldn't retrieve the data from headers.")
}

fileNameHeader := resp.Header.Get("Content-Disposition")
if fileNameHeader == "" {
return fmt.Errorf("couldn't retrieve Content-Disposition header")
}
splitFileNameHeader := strings.Split(fileNameHeader, "=")

defaultPath, err := config.GetPathToCalculation(path, splitFileNameHeader[1])
if err != nil {
logrus.WithError(err).Fatal("Couldn't get the path user added to download the calculations.")
}

body, responseError, err := request("GET", globalConfig.APIURL+"/calculations/results?teff="+fmt.Sprintf("%0.1f", teff)+"&logg="+fmt.Sprintf("%0.2f", logG), bytes.NewBuffer(nil))
if err != nil {
logrus.WithError(err).Fatal("error while perfoming the http request")
} else if responseError != nil {
logrus.WithFields(logrus.Fields{"message": responseError.Message, "status_code": responseError.StatusCode}).Fatal("errors occurred")
}

err = config.CreateAndWriteFile(body, defaultPath)
if err != nil {
logrus.WithError(err).Fatal("Something went wrong while creating or writing to a file.")
}

logrus.Info("The calculations were downloaded into: ", defaultPath)

return nil
}

func getCalculationResultByID(c *cli.Context) error {
args := os.Args
calcID := args[len(args)-1]

path := c.String("results-download-path")

resp, err := http.Get(globalConfig.APIURL + "/calculations/results/" + calcID)
if err != nil {
return fmt.Errorf("couldn't retrieve Content-Disposition header")
}

fileNameHeader := resp.Header.Get("Content-Disposition")
if fileNameHeader == "" {
return fmt.Errorf("couldn't retrieve Content-Disposition header")
}
splitFileNameHeader := strings.Split(fileNameHeader, "=")

defaultPath, err := config.GetPathToCalculation(path, splitFileNameHeader[1])
if err != nil {
logrus.WithError(err).Fatal("Couldn't get the path user added to download the calculations.")
}

body, responseError, err := request("GET", globalConfig.APIURL+"/calculations/results/"+calcID, bytes.NewBuffer(nil))
if err != nil {
logrus.WithError(err).Fatal("error while perfoming the http request")
} else if responseError != nil {
logrus.WithFields(logrus.Fields{"message": responseError.Message, "status_code": responseError.StatusCode}).Fatal("errors occurred")
}

err = config.CreateAndWriteFile(body, defaultPath)
if err != nil {
logrus.WithError(err).Fatal("Something went wrong while creating or writing to a file.")
}

logrus.Info("The calculations were downloaded into: ", defaultPath)

return nil
}

func createCalculation(c *cli.Context) error {
teff := c.Float64("teff")
logG := c.Float64("logG")
Expand Down Expand Up @@ -222,6 +300,10 @@ func main() {
Name: "logG",
Usage: "specifies the logG value when creating a calculation.",
},
&cli.StringFlag{
Name: "results-download-path",
Usage: "Specified path to the downloaded calculation results.",
},
},
Commands: []*cli.Command{
{
Expand Down Expand Up @@ -249,6 +331,21 @@ func main() {
Usage: "",
Action: getCalculations,
},

{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets just have one command that belongs in the calculation command. I would expect for the user to just type ccboc get calculation results {id} or ccboc --logG=4.0 --teff=1000 get calculation results

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gszasz We could use your insight as a user in this one...

Name: "results",
Aliases: []string{"res"},
Usage: "get results of calculations",
Action: func(c *cli.Context) error {
var err error
if c.Float64("teff") == 0 || c.Float64("logG") == 0 {
err = getCalculationResultByID(c)
} else {
err = getCalculationResult(c)
}
return err
},
},
},
},
{
Expand Down
36 changes: 36 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/url"
"os"
"os/user"
"path/filepath"
)
Expand Down Expand Up @@ -49,3 +50,38 @@ func GetDefaultConfigPath() (string, error) {

return filepath.Join(home, DefaultConfigPath, DefaultConfigFileName), nil
}

func GetPathToCalculation(defaultPath string, fileName string) (string, error) {
user, err := user.Current()
if err != nil {
return "", fmt.Errorf("couldn't get user's information: %v", err)
}
home := user.HomeDir

if defaultPath == "" {
return filepath.Join(home, DefaultConfigPath, fileName), nil
}

_, err = os.Stat(defaultPath)
if err != nil {
return "", fmt.Errorf("couldn't stat path %s: %v", defaultPath, err)
}
path := filepath.Join(defaultPath, filepath.Base(fileName))

return path, nil
}

func CreateAndWriteFile(body []byte, defaultPath string) error {
file, err := os.Create(defaultPath)
if err != nil {
return fmt.Errorf("couldn't create the calculation result file")
}

defer file.Close()

_, err = file.Write(body)
if err != nil {
return fmt.Errorf("couldn't write the data into the tar file")
}
return nil
}