devops

Go로 간단한 블록체인 구현하기 - CLI 본문

DevOps/Chain

Go로 간단한 블록체인 구현하기 - CLI

vata500 2022. 12. 12. 23:23
반응형

내가 구현한 블록체인과 통신할 수 있는 CLI를 구현해본다. flag를 사용해서 port와 사용할 mode를 설정한다.

package cli

import (
	"flag"
	"fmt"
	"os"

)

func usage() {
	fmt.Printf("This is vatacoin\n\n")
	fmt.Printf("Please use the following flags:\n\n")
	fmt.Printf("-port:		Set the PORT of the server\n")
	fmt.Printf("-mode:		Choose between 'html' and 'rest'\n\n")
	os.Exit(0)
}

func Start() {
	if len(os.Args) == 1 {
		usage()
	}

	port := flag.Int("port", 4000, "Set port of the server")
	mode := flag.String("mode", "rest", "Choose between 'html' and 'rest'")

	flag.Parse()

	switch *mode {
	case "rest":
		rest.Start(*port)
	case "html":
		fmt.Println("there is no explorer")
	default:
		usage()
	}
}
port := flag.Int("port", 4000, "Set port of the server")

port는 int로 선언하고, default 값은 4000으로 설정한다.

mode := flag.String("mode", "rest", "Choose between 'html' and 'rest'")

	flag.Parse()

	switch *mode {
	case "rest":
		rest.Start(*port)
	case "html":
		fmt.Println("there is no explorer")
	default:
		usage()
	}

mode에서는 string으로 선언하고, default 값은 rest api로 설정한다. 아래 switch 문을 통해서 rest, html 중 입력값을 통해 함수를 호출한다.

반응형
Comments