Skip to content

Commit

Permalink
SciCat CLI v3.0.0 alpha (#85)
Browse files Browse the repository at this point in the history
* move individual cli main.go's into commands subfolder

* add fundamental cobra code

* adapt main functions into cobra commands

* update dependencies, readme and goreleaser

* rewrite tests for cobra

* set goreleaser to prerelease mode if the version in the tag is non-final

* add version exception to argument count checking
  • Loading branch information
consolethinks authored Jun 26, 2024
1 parent 75a3760 commit 60e2957
Show file tree
Hide file tree
Showing 36 changed files with 2,208 additions and 2,583 deletions.
79 changes: 7 additions & 72 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,90 +21,22 @@ before:
- go generate ./...

builds:
- id: "datasetIngestor"
- id: "scicat-cli"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}} # This will set the VERSION variable in the binary to the github tag
- "-s -w -X 'github.com/paulscherrerinstitute/scicat/cmd/commands.VERSION={{.Version}}'"
env:
- CGO_ENABLED=0
dir: ./cmd/datasetIngestor/
dir: ./cmd/
goos:
- linux
- windows
- darwin
goarch:
- amd64
main: .
binary: datasetIngestor

- id: "datasetArchiver"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetArchiver/
goos:
- linux
- windows
- darwin
goarch:
- amd64
main: .
binary: datasetArchiver

- id: "datasetRetriever"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetRetriever/
goos:
- linux
- windows
- darwin
goarch:
- amd64
main: .
binary: datasetRetriever

- id: "datasetCleaner"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetCleaner/
goos:
- linux
- windows
- darwin
goarch:
- amd64
main: .
binary: datasetCleaner

- id: "datasetGetProposal"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetGetProposal/
goos:
- linux
- windows
- darwin
goarch:
- amd64
main: .
binary: datasetGetProposal
binary: scicat-cli


archives:
Expand All @@ -129,3 +61,6 @@ changelog:
exclude:
- "^docs:"
- "^test:"

release:
prerelease: auto
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

## Building

For testing, just build `main.go` for each command:
### General Informations

For testing, build the CLI tool as follows:

```
go build -o cmd/datasetIngestor/datasetIngestor cmd/datasetIngestor/main.go
cd cmd
go build -o scicat-cli
```

All applications are built automatically and can be downloaded from the [Releases](https://github.com/paulscherrerinstitute/scicat-cli/releases) section of this repo.
The CLI is built automatically and can be downloaded from the [Releases](https://github.com/paulscherrerinstitute/scicat-cli/releases) section of this repo.

To build the applications and target architectures locally, use GoReleaser. Check `.goreleaser.yaml` to see the configurations.
To use GoReleaser, you can run the command `goreleaser release --snapshot --clean` in your terminal. This will build the binaries, create the archives and generate the changelog. The `--snapshot flag` ensures that no publishing will happen.
Expand Down
46 changes: 46 additions & 0 deletions cmd/commands/argChecks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func exactArgsWithVersionException(n int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
version, _ := cmd.Flags().GetBool("version")
if version {
return nil
}
if len(args) != n {
return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
}
return nil
}
}

func minArgsWithVersionException(n int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
version, _ := cmd.Flags().GetBool("version")
if version {
return nil
}
if len(args) < n {
return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
}
return nil
}
}

func rangeArgsWithVersionException(min int, max int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
version, _ := cmd.Flags().GetBool("version")
if version {
return nil
}
if len(args) < min || len(args) > max {
return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))
}
return nil
}
}
Loading

0 comments on commit 60e2957

Please sign in to comment.