Skip to content

Commit

Permalink
Merge pull request #245 from gauravgahlot/cmd-gidari
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez authored Oct 11, 2022
2 parents cfa8b21 + 5facc0f commit 7485227
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ bazel-bin
bazel-out
bazel-testlogs
bazel-driver
gidari
gidari-cli
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export GO111MODULE=on

default:
chmod +rwx scripts/*.sh
$(GC) build cmd/gidari.go
$(GC) build -o gidari-cli cmd/gidari/main.go

# containers build the docker containers for performing integration tests.
.PHONY: containers
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Gidari is a "web-to-storage" tool for querying web APIs and persisting the resul

## Installation

TODO
```sh
go install github.com/alpine-hodler/gidari/cmd/gidari@latest
```

## Usage

Expand Down
1 change: 0 additions & 1 deletion cmd/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions cmd/bash-completion.sh

This file was deleted.

78 changes: 0 additions & 78 deletions cmd/gidari.go

This file was deleted.

89 changes: 89 additions & 0 deletions cmd/gidari/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2022 The Gidari 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
package cmd

import (
"context"
"errors"
"os"
"strings"

"github.com/alpstable/gidari"
"github.com/alpstable/gidari/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
flagConfig = "config"
flagVerbose = "verbose"
)

func RootCommand() *cobra.Command {
var (
configFilepath string // configFilepath is the path to the configuration file.
verbose bool // verbose is a flag that enables verbose logging.
)

rootCMD := &cobra.Command{
Long: "Gidari is a tool for querying web APIs and persisting resultant data onto local storage\n" +
"using a configuration file.",

Use: "gidari",
Short: "Persist data from the web to your database",
Example: "gidari --config config.yaml",
Version: version.Gidari,
PreRunE: func(cmd *cobra.Command, args []string) error {
value, err := cmd.Flags().GetString(flagConfig)
if err != nil {
//nolint:wrapcheck // need not wrap the error
return err
}

if !strings.HasSuffix(value, ".yaml") && !strings.HasSuffix(value, ".yml") {
//nolint:goerr113 // don't have static error
return errors.New("configuration must be a YAML document")
}

return nil
},
RunE: func(_ *cobra.Command, args []string) error { return runE(configFilepath, verbose) },
}

rootCMD.PersistentFlags().StringVarP(&configFilepath, flagConfig, "c", "", "path to configuration")
rootCMD.PersistentFlags().BoolVar(&verbose, flagVerbose, false, "print log data as the binary executes")

_ = rootCMD.MarkPersistentFlagRequired(flagConfig)

return rootCMD
}

//nolint:wrapcheck // need not wrap the error
func runE(configFilepath string, verbose bool) error {
file, err := os.Open(configFilepath)
if err != nil {
return err
}

cfg, err := gidari.NewConfig(context.Background(), file)
if err != nil {
return err
}

if verbose {
cfg.Logger.SetOutput(os.Stdout)
cfg.Logger.SetLevel(logrus.InfoLevel)
}

err = gidari.Transport(context.Background(), cfg)
if err != nil {
return err
}

return nil
}
22 changes: 22 additions & 0 deletions cmd/gidari/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022 The Gidari 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
package main

import (
"fmt"
"os"

"github.com/alpstable/gidari/cmd/gidari/cmd"
)

func main() {
if err := cmd.RootCommand().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}

0 comments on commit 7485227

Please sign in to comment.