This page looks best with JavaScript enabled

Getting Started With Go Getter

 ·  โ˜• 2 min read  ·  โœ๏ธ Allee Clark

Getting started with go getter
This post is a small example of how to use Hashicorp’s go-getter. This package allows you to pull files easily from GCS, GIT or S3. I’ll explain a bit about my use case below. I assume you know about Hashicorp and golang and looking for a clean example to get started. For the past few weeks, I’ve written designs for a delivery system using Hashi(Hashicorp) tools such as Consul for keys and service discovery, Vault for dynamic secrets, and Terraform to create and modify infrastructure resources. I’m determining where I should store Terraform files and directories. I’m experimenting allowing clients to commit Terraform folders to a repository such as git or s3. Committing a Terraform directory creates a new infrastructure request to eventually get to the terraform apply phase. This may also provide a testing path to test out new terraform modules, upgrades, etc. The create request will clone the folder to a remote server and run terraform init, terraform plan, terraform apply. Below is an example of using the โ€œGitGetterโ€ for retrieving a subdirectory of a git repository using go-getter. This package is also an easier interface to git commands instead of dealing with the c binding for the git2go package. I haven’t read through all the code, however I believe go-getter clones the entire respoitory in memory and then returns you the directories you specify. In Part two I will add an example of invoking Terraform directly using it’s packages.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
	"context"
	"fmt"
	"os"

	getter "github.com/hashicorp/go-getter"
)

func main() {
	client := &getter.Client{
		Ctx: context.Background(),
		//define the destination to where the directory will be stored. This will create the directory if it doesnt exist
		Dst: "/tmp/gogetter",
		Dir: true,
		//the repository with a subdirectory I would like to clone only
		Src:  "github.com/hashicorp/terraform/examples/cross-provider",
		Mode: getter.ClientModeDir,
		//define the type of detectors go getter should use, in this case only github is needed
		Detectors: []getter.Detector{
			&getter.GitHubDetector{},
		},
		//provide the getter needed to download the files
		Getters: map[string]getter.Getter{
			"git": &getter.GitGetter{},
		},
	}
	//download the files
	if err := client.Get(); err != nil {
		fmt.Fprintf(os.Stderr, "Error getting path %s: %v", client.Src, err)
		os.Exit(1)
	}
	//now you should check your temp directory for the files to see if they exist
}
Share on

allee
WRITTEN BY
Allee Clark