작성일: 2024년 4월 11일
Go 언어를 사용해서 CLI를 개발할 일이 생겼는데, 기존 C언어로 개발하던 개발 습성 때문인지 CLI를 개발할 생각하니까 귀찮고 짜증부터 났다.
Java처럼 CLI를 쉽게 개발할 수 있는 Go Package가 있지 않을까 싶어서 구글링을 해보니, 역시나 GoLang은 개발 도구나 Library package가 훌륭하다는 것을 또 한번 느끼게 되었다.
일단 딱 눈에 들어온 것은 Cobra library였다.
https://pkg.go.dev/github.com/spf13/cobra#section-readme
cobra package - github.com/spf13/cobra - pkg.go.dev
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used. Deprecated: Use SetOut and/or SetErr instead
pkg.go.dev
Cobra is a library providing a simple interface to create powerful modern CLI interfaces similar to git & go tools.
Cobra is also an application that will generate your application scaffolding to rapidly develop a Cobra-based application.
위 Cobra Web Docs 문서에서 소개하는 것처럼 Cobra는 library이면서, application scaffoling을 만들어주는 개발 도구이다.
그래서 단순하게 library reference만 읽고 사용법을 익히는 것이 아니라 Cobra 도구를 이용해서 scaffolding을 만들고, 그 scaffolding 안에서 나의 logic을 추가해야 한다.
Cobra의 개발 절차만 잘 따라하면, 시간을 팍팍 줄여가면서 CLI를 붕어빵 찍어내듯이 만들 수 있을 것 같은 느낌적인 느낌이 들었다.
그럼 그 개발 절차를 자세히 들여다 보면 이렇다.
https://www.youtube.com/watch?v=so3VZwdWcBg&t=4s
Reference
https://yjwang.tistory.com/137
[Go-lang] Cobra를 사용해서 cli 프로그램 개발
cobra 사용해서 cli tool을 개발해보고자 한다. 아무래도 Infra 작업을 하다보면 cli tool이 있으면 편하겠다는 생각을 하곤하는데 생각한 김에 만들어보고자합니다. cobra 프로젝트는 kubectl에서도 사용
yjwang.tistory.com
https://www.sktenterprise.com/bizInsight/blogDetail/dev/2755
[Golang] Cobra를 이용한 CLI 유틸리티 만들기 | 개발자 Story | SKT Enterprise
현대사회에서 대부분의 유저들은 휴대폰이나 컴퓨터 화면을 통해 쉽고 편한 GUI(Graphic User Interface)를 선호합니다. 그러나 GUI로는 채우기 힘든 여전히 CLI(Command Line Interface)를 선호하는 분야도 많
www.sktenterprise.com
https://nangman14.tistory.com/97
Go로 커맨드를 실행할 수 있는 CLI를 구현해보자 (With Cobra)
CLI(Command Line Interface)란 터미널을 통해 사용자와 컴퓨터가 상호작용하는 인터페이스를 말합니다. CLI는 그래픽을 통해 직관적으로 사용할 수 있는 GUI(Graphic User Interface)와 달리 명령줄로만 입력을
nangman14.tistory.com
기타: 다른 방식의 CLI 개발 예제
https://dev.to/tidalmigrations/interactive-cli-prompts-in-go-3bj9
Interactive CLI prompts in Go
Tidal Migrations 💓 CLI applications Do you like CLI applications? We love them! At Tidal...
dev.to
https://github.com/manifoldco/promptui
GitHub - manifoldco/promptui: Interactive prompt for command-line applications
Interactive prompt for command-line applications. Contribute to manifoldco/promptui development by creating an account on GitHub.
github.com
'Golang' 카테고리의 다른 글
go build 할 때 app version, build date, dev note를 추가하는 방법 (0) | 2022.07.05 |
---|---|
Go Package 및 Module 사용 예제 (0) | 2022.03.06 |
Golang REST API (0) | 2022.02.11 |
golang cross compile (0) | 2021.11.03 |
Golang으로 App 개발시 폴더 초기화 작업(go mod init 및 tidy) (0) | 2021.06.25 |