{"id":195,"date":"2018-11-10T17:21:53","date_gmt":"2018-11-10T17:21:53","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=195"},"modified":"2019-07-21T13:48:23","modified_gmt":"2019-07-21T13:48:23","slug":"write-a-cli-tool-with-t-d-d-weather-cli","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-go-tutorial\/write-a-cli-tool-with-t-d-d-weather-cli\/","title":{"rendered":"Write a CLI tool with T.D.D (Weather CLI)"},"content":{"rendered":"<p>Through these instructions, you will learn how to initiate a CLI tool using <strong>Golang<\/strong>. The CLI tool we are going to create step by step is a Weather CLI that helps us to get different attributes of the current weather including the current weather description, temperature and humidity.<\/p>\n<p>In this tutorial example, our CLI tool is dependent on a weather API service from&nbsp;<a href=\"https:\/\/openweathermap.org\/api\">https:\/\/openweathermap.org\/api<\/a> which is accessible through internet.<\/p>\n<p>Here is the step by step help:<\/p>\n<ol>\n<li>Like other Golang projects, you need to create your project under <strong>$GOPATH\/src<\/strong>. I&#8217;m creating my gotutorial projects like this:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ mkdir -p $GOPATH\/src\/github.com\/gotutorial\/weather-cli\r\n$ cd $GOPATH\/src\/github.com\/gotutorial\/weather-cli<\/pre>\n<\/li>\n<li>Create the weather-cli main go file which is the starting point of your application. I called it <strong>weather.go<\/strong>, you can name it whatever you like.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ vi weather.go<\/pre>\n<\/li>\n<li>Your main file should have at least a line defining the package that <strong>main<\/strong> function is in it. <strong>main<\/strong> function is the starting point of your Go application.<br \/>\nAdd following content to <strong>weather.go<\/strong> file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package main\r\n\r\nfunc main() {\r\n}<\/pre>\n<\/li>\n<li>I used&nbsp;<a href=\"https:\/\/github.com\/urfave\/cli\">https:\/\/github.com\/urfave\/cli<\/a> golang library to implement this CLI project.&nbsp;<br \/>\nAdd following import and instantiate the CLI application like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package main \r\n\r\nimport (\r\n\t\"github.com\/urfave\/cli\"\r\n)\r\n\r\nfunc main() {\r\n     app := cli.NewApp()\r\n}<\/pre>\n<\/li>\n<li>You can set a value to <strong>app.<span class=\"pl-smi\">Name<\/span><\/strong>&nbsp;attribute which identifies your CLI application name. <strong>app.Usage<\/strong> is the usage description of your CLI app and&nbsp;<strong>app.Version<\/strong> specifies your CLI app version.&nbsp;Add&nbsp;<strong>app.Run(os.Args)<\/strong> to run your CLI application\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package main \r\n\r\nimport (\r\n\t\"github.com\/urfave\/cli\"\r\n)\r\n\r\nfunc main() {\r\n     app := cli.NewApp() \/\/ Instantiate a new CLI application\r\n     app.Name = \"Weather CLI Tool\" \/\/ Assign the name of the CLI app\r\n     app.Usage = \"Prompt the current weather condition for a city\" \/\/ Assign the usage description for the CLI \r\n     app.Version = \"1.0.0\" \/\/Assign the version of the CLI app\r\n\r\n     app.Run(os.Args)\r\n}\r\n&nbsp;<\/pre>\n<\/li>\n<li>Build you application using following command:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ go build -o weather<\/pre>\n<\/li>\n<li>An executable file will be generated as the result. Run it like this and you see the help and usage information as follow:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ .\/weather\r\nNAME:\r\n   Weather CLI Tool - Prompt the current weather condition for a city\r\n\r\nUSAGE:\r\n   weather [global options] command [command options] [arguments...]\r\n\r\nVERSION:\r\n   1.0.0\r\n\r\nCOMMANDS:\r\n     help, h  Shows a list of commands or help for one command\r\n\r\nGLOBAL OPTIONS:\r\n   --help, -h     show help\r\n   --version, -v  print the version<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Through these instructions, you will learn how to initiate a CLI tool using Golang. The CLI tool we are going to create step by step is a Weather CLI that helps us to get different attributes of the current weather including the current weather description, temperature and humidity. In this tutorial example, our CLI tool [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":131,"menu_order":5,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-195","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":14,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/195\/revisions"}],"predecessor-version":[{"id":226,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/195\/revisions\/226"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/131"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}