From f605aaeed92b371ac76ed92d918bbc2907f18518 Mon Sep 17 00:00:00 2001 From: daz-3ux Date: Wed, 6 Sep 2023 15:50:45 +0800 Subject: [PATCH] feat: init cobra Signed-off-by: daz-3ux --- Makefile | 12 ++++----- api/openapi/openapi.yaml | 1 - cmd/dBlog/main.go | 13 ++++++++-- cmd/go.mod | 3 --- go.mod | 10 ++++++++ go.sum | 21 ++++++++++++++++ internal/dazBlog/dazBlog.go | 49 +++++++++++++++++++++++++++++++++++++ 7 files changed, 97 insertions(+), 12 deletions(-) delete mode 100644 cmd/go.mod create mode 100644 go.sum create mode 100644 internal/dazBlog/dazBlog.go diff --git a/Makefile b/Makefile index 6fc4a9e..25bb579 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/api/openapi/openapi.yaml b/api/openapi/openapi.yaml index f52896b..a87243f 100644 --- a/api/openapi/openapi.yaml +++ b/api/openapi/openapi.yaml @@ -42,7 +42,6 @@ paths: '400': description: request failed due to client-side problem content: - application/json: application/json: schema: $ref: '#/components/schemas/ErrResponse' diff --git a/cmd/dBlog/main.go b/cmd/dBlog/main.go index 219572f..fac19c8 100644 --- a/cmd/dBlog/main.go +++ b/cmd/dBlog/main.go @@ -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) + } } diff --git a/cmd/go.mod b/cmd/go.mod deleted file mode 100644 index 0327993..0000000 --- a/cmd/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module main - -go 1.21.0 diff --git a/go.mod b/go.mod index fb230b1..2d89597 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..58d4bdf --- /dev/null +++ b/go.sum @@ -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= diff --git a/internal/dazBlog/dazBlog.go b/internal/dazBlog/dazBlog.go new file mode 100644 index 0000000..c95bd83 --- /dev/null +++ b/internal/dazBlog/dazBlog.go @@ -0,0 +1,49 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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 +}