Go with Cobra + Viper CLI for Parsing Text Files
Tags: cobra, viper, go
Published: 2018-12-06 18:12:14

---
title: Go with Cobra + Viper CLI for Parsing Text Files
published: true
description: Article write up of coding with Go using Cobra + Viper CLI for Parsing Text Files. Video included of coding.
tags: cobra, viper, go, golang
---

<em>Part 1 of 3</em> - Coding Session in Go - Cobra + Viper CLI for Parsing Text Files, Retrieval of Twitter Data, Exports to various file formats, and export to Apache Cassandra.

<strong>UPDATED PARTS: (Goes to original Composite Code blog posts)</strong>
<ol>
	<li><a href="http://compositecode.blog/2018/12/04/twitz-coding-session-in-go-cobra-viper-cli-for-parsing-text-files/">Twitz Coding Session in Go - Cobra + Viper CLI for Parsing Text Files (this post)</a></li>
	<li><a href="http://compositecode.blog/2018/12/07/twitz-coding-session-in-go-cobra-viper-cli-with-initial-twitter-cassandra-installation/">Twitz Coding Session in Go - Cobra + Viper CLI with Initial Twitter + Cassandra Installation</a></li>
	<li><a href="http://compositecode.blog/2018/12/27/twitz-coding-session-in-go-cobra-viper-cli-wrap-up-twitter-data-retrieval/">Twitz Coding Session in Go - Cobra + Viper CLI Wrap Up + Twitter Data Retrieval</a></li>
</ol>

{% youtube 1iniLntln2A %}

<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=220s">3:40</a></strong> Stated goals of application. I go through a number of features that I intend to build into this application, typing them up in a text doc. The following are the items I added to the list.
<ol>
	<li>The ability to import a text file of Twitter handles that are mixed in among a bunch of other text.</li>
	<li>The ability to clean up that list.</li>
	<li>The ability to export the cleaned up list of handles to a text, csv, JSON, xml, or plain text file.</li>
	<li>The ability, using the Twitter API, to pull data form the bio, latest tweets, and other related information.</li>
</ol>
<img class="  wp-image-15012 alignright" src="https://compositecode.files.wordpress.com/2018/12/github.png" alt="github" width="222" height="222" /><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=506s"><strong>8:26</strong></a> Creating a new Github Repo. A few tips and tricks with creating a new repo with the Github.
<ul>
	<li>Github Repo: <a href="https://github.com/Adron/twitz" target="_blank" rel="noopener">Twitz</a></li>
	<li>License: <a href="https://github.com/Adron/twitz/blob/master/LICENSE" target="_blank" rel="noopener">Apache-2.0</a></li>
	<li>Issue Filing Options: <a href="https://github.com/Adron/twitz/issues/new?template=feature_request.md" target="_blank" rel="noopener">Feature Request</a> or <a href="https://github.com/Adron/twitz/issues/new?template=bug_report.md" target="_blank" rel="noopener">Bug Report</a>.</li>
	<li><a href="https://github.com/Adron/twitz/blob/master/README.md" target="_blank" rel="noopener">README.md</a></li>
	<li>The <a href="https://github.com/Adron/twitz/blob/master/_config.yml" target="_blank" rel="noopener">Jeckyll yml file</a> that provides the skinning for the <a href="https://adron.github.io/twitz/" target="_blank" rel="noopener">project page</a> at <a href="https://adron.github.io/twitz/" target="_blank" rel="noopener">https://adron.github.io/twitz/</a>.</li>
</ul>
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=586s"><strong>9:46</strong></a> Twitz is brought into existence! Woo
~~ Have to reset up my SSH key real quick. This is a quick tutorial if you aren't sure how to do it, otherwise skip forward to the part where I continue with project setup and initial coding.

<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=760s"><strong>12:40</strong></a> Call out for @justForFunc and Francesc's episode on Cobra!

Check out @Francesc's "Just for Func". It's a great video/podcast series of lots and lots of Go!
<ul>
	<li><a href="https://twitter.com/justforfunc" target="_blank" rel="noopener">@JustforFunc</a> on Twitter.</li>
	<li><a href="https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw" target="_blank" rel="noopener">Just for Func on Youtube</a>.</li>
	<li>The episode I mentioned regarding the <a href="https://youtu.be/WvWPGVKLvR4" target="_blank" rel="noopener">Cobra CLI Library</a>.</li>
</ul>
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=782s"><strong>13:02</strong></a> Back to project setup. Cloning repo, getting initial README.emd, .gitignore file setup, and related collateral for the basic project. I also add Github's "issues" files via the Github interface and rebase these changes in later.
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=860s"><strong>14:20</strong></a> Adding some options to the .gitignore file.
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=920s"><strong>15:20</strong></a> Set dates and copyright in license file.
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=960s">16:00</a></strong> Further setup of project, removing WIKIs, projects, and reasoning for keeping issues.
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=1013s"><strong>16:53</strong></a> Opening Goland up after an update. Here I start going through the specific details of how I setup a Go Project in Goland, setting configuration and related collateral.
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=1034s"><strong>17:14</strong></a> Setup of Goland's `golang` Command Line Launcher.
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=1545s">25:45</a></strong> Introduction to Cobra (and first mention of Viper by association).
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=1603s">26:43</a></strong> Installation of Cobra with `go get github.com/spf13/cobra/cobra/` and the gotchas (remember to have your paths set, etc).
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=1790s"><strong>29:50</strong></a> Using Cobra CLI to build out the command line interface app's various commands.
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=2103s"><strong>35:03</strong></a> Looking over the generated code and commenting on the comments and their usefulness.
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=2160s">36:00</a> </strong>Wiring up the last bits of Cobra with some code, via main func, for CLI execution.
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=2887s">48:07</a> </strong>I start wiring up the Viper configuration at this point. Onward from here it's coding, coding, and configuration, and more coding.
<h2>Implementing the `twitz config` Command</h2>
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=4040s">1:07:20</a></strong> Confirming config and working up the `twitz config` command implementation.
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=4240s">1:10:40</a> </strong>First execution of `twitz config` and I describe where and how Cobra's documentation strings work through the `--help` flag and is put into other help files based on the need for the short or long description.

The basic config code when implemented looked something like this end of session. This snippet of code doesn't do much beyond provide the displace of the configuration variables from the configuration file. Eventually I'll add more to this file so the CLI can be easier to debug when setting it up.

```
package cmd

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

// configCmd represents the config command
var configCmd = &cobra.Command{
	Use:   "config",
	Short: "A brief description of your command",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For the custom example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("Twitterers File: %s\n", viper.GetString("file"))
		fmt.Printf("Export File: %s\n", viper.GetString("fileExport"))
		fmt.Printf("Export Format: %s\n", viper.GetString("fileFormat"))<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0;"></span>
	},
}

func init() {
	rootCmd.AddCommand(configCmd)
}
```

<h2>Implementing the `twitz parse` Command</h2>
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=4450s"><strong>1:14:10</strong></a> Starting on the implementation of the `twitz parse` command.
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=4584s">1:16:24</a> </strong>Inception of my "helpers.go" file to consolidate and clean up some of the code.
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=5182s"><strong>1:26:22</strong></a> REDEX Implementation time!
<strong><a class="yt-simple-endpoint style-scope yt-formatted-string" href="https://www.youtube.com/watch?v=1iniLntln2A&t=5532s">1:32:12</a></strong> Trying out the REGEX101.com site.

I'll post the finished code and write up some details on the thinking behind it when I post video two of this application development sessions.

That's really it for this last session. Hope the summary helps for anybody working through building CLI apps with Go and Cobra. Until next session, happy thrashing code.

I'm on Twitter [@Adron](https://twitter.com/Adron) and Twitch [@adronhall](https://twitch.tv/adronhall/) listening to metal all the time and coding, writing about coding, learning, and teaching all sorts of topics from database tech to coding chops. Thanks for reading!