Skip to content

Commit

Permalink
refactor: change structure and fix lint warnings (#3)
Browse files Browse the repository at this point in the history
* refactor: change structure, fix lint warnings and enhance workflows
  • Loading branch information
juniorgarcia authored Nov 6, 2024
1 parent 1d30504 commit c898d48
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 37 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
on: pull_request
on:
pull_request:
push:
branches:
main
jobs:
configure:
runs-on: ubuntu-20.04
strategy:
matrix:
go-version: [ '1.15', '1.23' ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: "^1.5"
- run: go version
test:
runs-on: ubuntu-20.04
needs: configure
steps:
- uses: actions/checkout@v2
- name: Testing lib
go-version: '^${{ matrix.go-version }}'
- name: Install deps
run: go mod tidy
- name: Test
run: make test
test_bench:
runs-on: ubuntu-20.04
needs: configure
steps:
- uses: actions/checkout@v2
- name: Testing with benchmarks
run: make test_bench
- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60.3
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

out
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
build:
go build -o ascii-banner
test:
go test ./banner
test_bench:
go test ./banner -bench=.
build: clean
go build -o ./out/ascii-banner ./cmd/...
build-prod: clean
go build -ldflags="-s -w" -o ./out/ascii-banner ./cmd/...
clean:
rm -rf ascii-banner
@rm -rf ./out
format:
go fmt ./...
test:
go test ./...
test-bench:
go test ./... -bench=.
13 changes: 7 additions & 6 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strconv"

"github.com/joho/godotenv"
"github.com/juniorgarcia/ascii-banner-cli/banner"
"github.com/juniorgarcia/ascii-banner-cli/pkg/banner"
)

const appName = "ASCII Banner Generator"
Expand All @@ -37,24 +37,25 @@ INDIVIDUAL_SIDE_PADDING=2

func createConfigFile(f string) error {
file, err := os.Create(f)
defer file.Close()

if err != nil {
log.Fatalf("Unable to create default config file at %s. Error: %s", f, err)
panic(err)
}

defer file.Close()

if _, err := file.Write(defaultConfigFileContent); err != nil {
log.Fatalf("Unable to write to config file: %s", err)
panic(err)
}

file.Sync()
_ = file.Sync()

return nil
}

func checkConfigFile(f string) bool {
func checkConfigFile() bool {
if _, err := os.Stat(configFilePath); errors.Is(err, fs.ErrNotExist) {
return false
}
Expand All @@ -63,7 +64,7 @@ func checkConfigFile(f string) bool {
}

func main() {
if !checkConfigFile(configFilePath) {
if !checkConfigFile() {
if err := createConfigFile(configFilePath); err != nil {
log.Fatal("Unable to create default config file!", err)
}
Expand All @@ -84,7 +85,7 @@ func main() {
d := flag.String("d", os.Getenv("DASHES_CHAR"), "The dashes character")
s := flag.String("s", os.Getenv("SIDE_CHAR"), "The sides character")

padding, err := strconv.Atoi(os.Getenv("INDIVIDUAL_SIDE_PADDING"))
padding, _ := strconv.Atoi(os.Getenv("INDIVIDUAL_SIDE_PADDING"))

p := flag.Int("p", padding, "Individual side padding")
h := flag.Bool("h", false, "Show help")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/juniorgarcia/ascii-banner-cli

go 1.15
go 1.23.0

require github.com/joho/godotenv v1.3.0
File renamed without changes.
6 changes: 3 additions & 3 deletions banner/banner_test.go → pkg/banner/banner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPrintHorizontalEdges(t *testing.T) {

func BenchmarkPrintHorizontalEdges(b *testing.B) {
for i := 0; i < b.N; i++ {
NewDefaultBanner("Hello, World").PrintHorizontalEdges(HorLineTypeBottom)
_, _ = NewDefaultBanner("Hello, World").PrintHorizontalEdges(HorLineTypeBottom)
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func TestPrintFullBanner(t *testing.T) {
b.Message = c.in
banner, err := b.PrintFullBanner()
if err != nil {
t.Errorf("PrintFullBanner() raised an error: %w", err)
t.Errorf("PrintFullBanner() raised an error: %+v", err)
}
if banner != c.out {
t.Errorf("PrintFullBanner() expected: \n\"%s\". \n Got: \n\"%s\"", c.out, banner)
Expand All @@ -90,6 +90,6 @@ func TestPrintFullBanner(t *testing.T) {

func BenchmarkPrintFullBanner(b *testing.B) {
for i := 0; i < b.N; i++ {
NewDefaultBanner("Hello, world!").PrintFullBanner()
_, _ = NewDefaultBanner("Hello, world!").PrintFullBanner()
}
}

0 comments on commit c898d48

Please sign in to comment.