Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
Signed-off-by: Janos Bonic <[email protected]>
Co-authored-by: James Humphries <[email protected]>
  • Loading branch information
Janos Bonic and Yantrio committed Jul 15, 2024
1 parent 16bacdf commit b663718
Show file tree
Hide file tree
Showing 29 changed files with 1,795 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) The OpenTofu Authors
# SPDX-License-Identifier: MPL-2.0, MIT
name: Verify
permissions: {}
on:
pull_request:
push:
jobs:
generate:
name: Go generate
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Check license headers
run: go run github.com/opentofu/tofudl/tools/license-headers -check-only
- name: Check GPG key
working-directory: branding
run: go run github.com/opentofu/tofudl/tools/bundle-gpg-key -check-only
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Lint
run: go run github.com/opentofu/tofudl/tools/lint
tests:
name: Tests
strategy:
matrix:
os: [ubuntu, windows, macos]
runs-on: ${{ matrix.os }}-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: Run tests
run: |
go test ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
*~
57 changes: 57 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- dupl
- durationcheck
- errcheck
- errname
- errorlint
- exhaustive
- exportloopref
- forbidigo
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- loggercheck
- makezero
- mirror
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tenv
- testableexamples
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# TofuDL: OpenTofu downloader library for Go with minimal dependencies

This library provides an easy way to download, verify, and unpack OpenTofu binaries for local use in Go. It has a minimal set of dependencies and is easy to integrate.

**Note:** This is not a standalone tool to download OpenTofu, it is purely meant to be used as Go library in support of other tools that need to run `tofu`. Please check the [installation instructions](https://opentofu.org/docs/intro/install/) for supported ways to perform an OpenTofu installation.

## Basic usage

The downloader will work without any extra configuration out of the box:

```go
package main

import (
"context"
"os"
"os/exec"
"runtime"

"github.com/opentofu/tofudl"
)

func main() {
// Initialize the downloader:
dl, err := tofudl.New()
if err != nil {
panic(err)
}

// Download the latest stable version
// for the current architecture and platform:
binary, err := dl.Download(context.TODO())
if err != nil {
panic(err)
}

// Write out the tofu binary to the disk:
file := "tofu"
if runtime.GOOS == "windows" {
file += ".exe"
}
if err := os.WriteFile(file, binary, 0755); err != nil {
panic(err)
}

// Run tofu:
cmd := exec.Command("./"+file, "init")
if err := cmd.Run(); err != nil {
panic(err)
}
}
```

## Advanced usage

Both `New()` and `Download()` accept a number of options. You can find the detailed documentation [here](https://pkg.go.dev/github.com/opentofu/tofudl).
66 changes: 66 additions & 0 deletions architecture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0

package tofudl

import (
"regexp"
"runtime"
)

// Architecture describes the architecture to download OpenTofu for. It defaults to the current system architecture.
type Architecture string

const (
// ArchitectureAuto is the default value and defaults to downloading OpenTofu for the current architecture.
ArchitectureAuto Architecture = ""
// Architecture386 describes the 32-bit Intel CPU architecture.
Architecture386 Architecture = "386"
// ArchitectureAMD64 describes the 64-bit Intel/AMD CPU architecture.
ArchitectureAMD64 Architecture = "amd64"
// ArchitectureARM describes the 32-bit ARM (v7) architecture.
ArchitectureARM Architecture = "arm"
// ArchitectureARM64 describes the 64-bit ARM (v8) architecture.
ArchitectureARM64 Architecture = "arm64"
)

var architectureRe = regexp.MustCompile("^[a-z0-9]*$")

// Validate returns an error if the platform is not a valid platform descriptor.
func (a Architecture) Validate() error {
if !architectureRe.MatchString(string(a)) {
return &InvalidArchitectureError{a}
}
return nil
}

// ResolveAuto resolves the value of ArchitectureAuto if needed based on the current runtime.GOARCH.
func (a Architecture) ResolveAuto() (Architecture, error) {
if a != ArchitectureAuto {
return a, nil
}
switch runtime.GOARCH {
case "386":
return Architecture386, nil
case "amd64":
return ArchitectureAMD64, nil
case "arm":
return ArchitectureARM, nil
case "arm64":
return ArchitectureARM64, nil
default:
return ArchitectureAuto, UnsupportedArchitectureError{
Architecture(runtime.GOARCH),
}
}
}

// ArchitectureValues returns all supported values for Architecture excluding ArchitectureAuto.
func ArchitectureValues() []Architecture {
return []Architecture{
Architecture386,
ArchitectureAMD64,
ArchitectureARM,
ArchitectureARM64,
}
}
3 changes: 3 additions & 0 deletions branding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Branding settings

This package contains a set of constants to change the built-in behavior and configuration of `tofudl`, such as the bundled GPG key, etc.
33 changes: 33 additions & 0 deletions branding/branding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0

package branding

// ProductName describes the name of the product being downloaded.
const ProductName = "OpenTofu"

// DefaultDownloadAPIURL describes the API serving the version and file information.
const DefaultDownloadAPIURL = "https://get.opentofu.org/tofu/api.json"

// DefaultMirrorURLTemplate is a Go template that describes the download URL with the {{ .Version }} and {{ .Artifact }}
// embedded into the URL.
const DefaultMirrorURLTemplate = "https://github.com/opentofu/opentofu/releases/download/v{{ .Version }}/{{ .Artifact }}"

// BinaryName holds the name of the binary in the artifact. This may be suffixed .exe on Windows.
const BinaryName = "tofu"

// GPGKeyURL describes the URL to download the bundled GPG key from. The GPG key bundler uses this to download the
// GPG key for verification.
const GPGKeyURL = "https://get.opentofu.org/opentofu.asc"

// GPGKeyFingerprint is the GPG key fingerprint the bundler should expect to find when downloading the key.
const GPGKeyFingerprint = "E3E6E43D84CB852EADB0051D0C0AF313E5FD9F80"

// SPDXAuthorsName describes the name of the authors to be attributed in copyright notices in this repository.
const SPDXAuthorsName = "The OpenTofu Authors"

// SPDXLicense describes the license for copyright attribution in this repository.
const SPDXLicense = "MPL-2.0"

// MaximumUncompressedFileSize indicates the maximum file size when uncompressed.
const MaximumUncompressedFileSize = 1024 * 1024 * 1024 * 1024
63 changes: 63 additions & 0 deletions branding/gpg_key.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b663718

Please sign in to comment.