Skip to content

Commit

Permalink
feat: init cobra
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Sep 6, 2023
1 parent 0a8fa24 commit f605aae
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ all: add-copyright format build

.PHONY: build
build: tidy # 编译源码,依赖 tidy 目标自动添加/移除依赖包.
@go build -v -o $(OUTPUT_DIR)/dBlog $(ROOT_DIR)/cmd/dBlog/main.go
go build -v -o $(OUTPUT_DIR)/dBlog $(ROOT_DIR)/cmd/dBlog/main.go

.PHONY: format
format: # 格式化 Go 源码.
@gofmt -s -w ./
gofmt -s -w ./

.PHONY: add-copyright
add-copyright: # 添加版权头信息.
@addlicense -v -f $(ROOT_DIR)/scripts/boilerplate.txt $(ROOT_DIR) --skip-dirs=third_party,vendor,$(OUTPUT_DIR)
addlicense -v -f $(ROOT_DIR)/scripts/boilerplate.txt $(ROOT_DIR) --skip-dirs=third_party,vendor,$(OUTPUT_DIR)

.PHONY: swagger
swagger: # 启动 swagger 在线文档.
@swagger serve -F=swagger --no-open --port 65534 $(ROOT_DIR)/api/openapi/openapi.yaml
swagger serve -F=swagger --no-open --port 65534 $(ROOT_DIR)/api/openapi/openapi.yaml

.PHONY: tidy
tidy: # 自动添加/移除依赖包.
@go mod tidy
go mod tidy

.PHONY: clean
clean: # 清理构建产物、临时文件等.
@-rm -vrf $(OUTPUT_DIR)
-rm -vrf $(OUTPUT_DIR)
1 change: 0 additions & 1 deletion api/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ paths:
'400':
description: request failed due to client-side problem
content:
application/json:
application/json:
schema:
$ref: '#/components/schemas/ErrResponse'
Expand Down
13 changes: 11 additions & 2 deletions cmd/dBlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@

package main

import "fmt"
import (
"os"

_ "go.uber.org/automaxprocs/maxprocs"

"github.com/Daz-3ux/dBlog/internal/dazBlog"
)

// program entry function
func main() {
fmt.Println("Hello dazBlog")
command := dazBlog.NewDazBlogCommand()
if err := command.Execute(); err != nil {
os.Exit(1)
}
}
3 changes: 0 additions & 3 deletions cmd/go.mod

This file was deleted.

10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module github.com/Daz-3ux/dBlog

go 1.21.0

require (
github.com/spf13/cobra v1.7.0
go.uber.org/automaxprocs v1.5.3
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
49 changes: 49 additions & 0 deletions internal/dazBlog/dazBlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package dazBlog

import (
"fmt"
"github.com/spf13/cobra"
)

func NewDazBlogCommand() *cobra.Command {
cmd := &cobra.Command{
// specify the name of the command
Use: "dBlog",
// specify the short description of the command
Short: "dBlog is a simple blog system",
// specify the long description of the command
Long: `dBlog is a simple but not easy blog system,
Find more dBlog information at:
https://github.com/daz-3ux/dBlog#readme`,

// when error occurs, the command will not print usage information
SilenceUsage: true,
// specify the run function to execute when cmd.Execute() is called
// if the function fails, an error message will be returned
RunE: func(cmd *cobra.Command, args []string) error {
return run()
},
//
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
if len(arg) > 0 {
return fmt.Errorf("%q does not take any arguments, got %q", cmd.CommandPath(), args)
}
}

return nil
},
}

return cmd
}

func run() error {
fmt.Println("Hello, dBlog!")
return nil
}

0 comments on commit f605aae

Please sign in to comment.