Skip to content

Commit

Permalink
setup actions and goreleaser for example cli
Browse files Browse the repository at this point in the history
  • Loading branch information
marianina8 committed Jun 17, 2023
1 parent 935c0d9 commit f69ed02
Show file tree
Hide file tree
Showing 47 changed files with 1,208 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: goreleaser

on:
push:
# run only against tags
tags:
- '*'

permissions:
contents: write
# packages: write
# issues: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v3
with:
go-version: stable
cache: true
# More assembly might be required: Docker logins, GPG, etc. It all depends
# on your needs.
- uses: goreleaser/goreleaser-action@v4
with:
# either 'goreleaser' (default) or 'goreleaser-pro':
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.PUBLISHER_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
# distribution:
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:latest

RUN apk add --no-cache go

COPY . .

CMD ["go", "test", "-v", "github.com/marianina8/cli/cmd"]
Empty file added LICENSE
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# cli
# Example CLI
54 changes: 54 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/marianina8/cli/utils"
"github.com/spf13/cobra"
)

// convertCmd represents the convert command
var convertCmd = &cobra.Command{
Use: "convert",
Short: "Converts a JSON to YAML file",
Run: func(cmd *cobra.Command, args []string) {
// converts json to yaml by default
file := cmd.Flag("file").Value.String()
silence, _ := cmd.Flags().GetBool("silence")
output := cmd.Flag("output").Value.String()

if file == "" {
fmt.Println("Please provide a JSON file to convert")
return
}
yamlData, err := utils.J2Ytransform(file)
if err != nil {
fmt.Fprint(cmd.ErrOrStderr(), "Error converting JSON to YAML")
os.Exit(1)
}

if !silence {
fmt.Fprint(cmd.OutOrStdout(), string(yamlData))
}

if output != "" {
err = ioutil.WriteFile(output, yamlData, 0644)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Error writing YAML file: %v", err)
os.Exit(1)
}
}
},
}

func init() {
rootCmd.AddCommand(convertCmd)
convertCmd.PersistentFlags().StringP("file", "f", "", "JSON file to convert")
convertCmd.PersistentFlags().BoolP("silence", "s", false, "Silence the output")
convertCmd.PersistentFlags().StringP("output", "o", "", "Output file name")
}
60 changes: 60 additions & 0 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"bytes"
"io"
"os"
"path/filepath"
"testing"

"github.com/marianina8/cli/models"
"gopkg.in/yaml.v2"
)

func TestConvert(t *testing.T) {
t.Run("Test successful convert", func(t *testing.T) {
b := bytes.NewBufferString("")
rootCmd.SetOut(b)
rootCmd.SetArgs([]string{"convert", "-f", "../example.json"})
err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
actualBytes, err := io.ReadAll(b)
if err != nil {
t.Fatal(err)
}
filePath := filepath.Join("..", "example.yaml")
expectedBytes, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
var workshop1, workshop2 models.Workshop
yaml.Unmarshal(actualBytes, &workshop1)
yaml.Unmarshal(expectedBytes, &workshop2)
if !(workshop1.Title == workshop2.Title &&
workshop1.Instructors[0].Name == workshop2.Instructors[0].Name &&
workshop1.Instructors[0].Email == workshop2.Instructors[0].Email &&
workshop1.Instructors[1].Name == workshop2.Instructors[1].Name &&
workshop1.Instructors[1].Email == workshop2.Instructors[1].Email) {
t.Fatalf("expected %v, got %v", workshop2, workshop1)
}
})

t.Run("Fail to convert", func(t *testing.T) {
b := bytes.NewBufferString("")
rootCmd.SetErr(b)
rootCmd.SetArgs([]string{"convert", "--random"})
err := rootCmd.Execute()
if err == nil {
t.Fatalf("expected err (unknown flag) but got nil")
}
actualBytes, err := io.ReadAll(b)
if err != nil {
t.Fatal(err)
}
if string(actualBytes) != "Error: unknown flag: --random\n" {
t.Fatal(err)
}
})
}
133 changes: 133 additions & 0 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"fmt"

"github.com/mum4k/termdash"
"github.com/mum4k/termdash/align"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/linestyle"
"github.com/mum4k/termdash/terminal/tcell"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/mum4k/termdash/widgets/button"
"github.com/mum4k/termdash/widgets/text"
"github.com/mum4k/termdash/widgets/textinput"
"github.com/spf13/cobra"
)

// dashboardCmd represents the dashboard command
var dashboardCmd = &cobra.Command{
Use: "dashboard",
Short: "An example dashboard",
Run: func(cmd *cobra.Command, args []string) {
terminalLayer, err := tcell.New(tcell.ColorMode(terminalapi.ColorMode256), tcell.ClearStyle(cell.ColorYellow, cell.ColorNavy))
if err != nil {
fmt.Println(err)
}
defer terminalLayer.Close()
value := make(chan string)
rollingText, err := text.New(text.RollContent())
if err != nil {
panic(err)
}
go rollText(rollingText, value)
leftContainer := container.Left(
container.Border(linestyle.Light),
container.PlaceWidget(rollingText),
)
input, err := textinput.New(
textinput.Label("Input: "),
textinput.MaxWidthCells(20),
textinput.PlaceHolder("Enter text here"),
)
if err != nil {
panic(err)
}
submitButton, err := button.New("Submit", func() error {
inputValue := input.ReadAndClear()
if inputValue != "" {
value <- inputValue
}
return nil
})
if err != nil {
panic(err)
}
clearButton, err := button.New("Clear", func() error {
rollingText.Reset()
return nil
})
if err != nil {
panic(err)
}
rightContainer := container.Right(
container.SplitHorizontal(
container.Top(
container.Border(linestyle.Light),
container.PlaceWidget(input),
container.AlignVertical(align.VerticalBottom),
container.MarginTop(3),
container.MarginBottom(3),
container.MarginRight(3),
container.MarginLeft(3),
),
container.Bottom(
container.SplitVertical(
container.Left(
container.Border(linestyle.Light),
container.PlaceWidget(submitButton),
container.AlignVertical(align.VerticalTop),
),
container.Right(
container.Border(linestyle.Light),
container.PlaceWidget(clearButton),
container.AlignVertical(align.VerticalTop),
),
),
),
),
)
containerLayer, _ := container.New(
terminalLayer,
container.SplitVertical(
leftContainer,
rightContainer,
container.SplitPercent(60),
),
)

ctx, cancel := context.WithCancel(context.Background())
quitter := func(k *terminalapi.Keyboard) {
if k.Key == 'q' || k.Key == 'Q' {
cancel()
}
}
if err := termdash.Run(ctx, terminalLayer, containerLayer, termdash.KeyboardSubscriber(quitter)); err != nil {
panic(err)
}

},
}

func init() {
rootCmd.AddCommand(dashboardCmd)
}

func rollText(rollingText *text.Text, value <-chan string) {
for {
select {
case v := <-value:
err := rollingText.Write(v)
if err != nil {
panic(err)
}
default:
// no value on the channel, continue without blocking
}
}
}
52 changes: 52 additions & 0 deletions cmd/jsonToYaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/marianina8/cli/utils"
"github.com/spf13/cobra"
)

// jsonToYamlCmd represents the jsonToYaml command
var jsonToYamlCmd = &cobra.Command{
Use: "jsonToYaml",
Short: "Converts json to yaml",
Aliases: []string{"j2y"},
Run: func(cmd *cobra.Command, args []string) {
file := cmd.Flag("file").Value.String()
silence, _ := cmd.Flags().GetBool("silence")
output := cmd.Flag("output").Value.String()

if file == "" {
fmt.Println("Please provide a JSON file to convert")
return
}
yamlData, err := utils.J2Ytransform(file)
if err != nil {
log.Fatalf("Error converting JSON to YAML: %v", err)
os.Exit(1)
}

if !silence {
fmt.Println(string(yamlData))
}

if output != "" {
err = ioutil.WriteFile(output, yamlData, 0644)
if err != nil {
log.Fatalf("Error writing YAML file: %v", err)
os.Exit(1)
}
}
},
}

func init() {
convertCmd.AddCommand(jsonToYamlCmd)
}
Loading

0 comments on commit f69ed02

Please # to comment.