Skip to content

Commit

Permalink
(maint) add deadcode check
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Oct 30, 2024
1 parent abae921 commit 7a9d2a2
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 267 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ jobs:
- name: Go Init
uses: ./.github/actions/go_init

- name: find deadcode
run: |
go install golang.org/x/tools/cmd/deadcode@latest
deadcode . | tee out && [ ! -s out ]
# get .golangci.yml from github.com/overmindtech/golangci-lint_config
- name: Get .golangci.yml from github.com/overmindtech/golangci-lint_configs
run: |
Expand Down
8 changes: 0 additions & 8 deletions cmd/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

// AuthenticatedApiKeyClient Returns an apikey client that uses the auth
// embedded in the context and otel instrumentation
func AuthenticatedApiKeyClient(ctx context.Context, oi sdp.OvermindInstance) sdpconnect.ApiKeyServiceClient {
httpClient := NewAuthenticatedClient(ctx, otelhttp.DefaultClient)
log.WithContext(ctx).WithField("apiUrl", oi.ApiUrl).Debug("Connecting to overmind apikeys API (pre-authenticated)")
return sdpconnect.NewApiKeyServiceClient(httpClient, oi.ApiUrl.String())
}

// UnauthenticatedApiKeyClient Returns an apikey client with otel instrumentation
// but no authentication. Can only be used for ExchangeKeyForToken
func UnauthenticatedApiKeyClient(ctx context.Context, oi sdp.OvermindInstance) sdpconnect.ApiKeyServiceClient {
Expand Down
47 changes: 0 additions & 47 deletions cmd/changes_submit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,6 @@ type plannedChangeGroups struct {
unsupported map[string][]*sdp.MappedItemDiff
}

func (g *plannedChangeGroups) NumUnsupportedChanges() int {
num := 0

for _, v := range g.unsupported {
num += len(v)
}

return num
}

func (g *plannedChangeGroups) NumSupportedChanges() int {
num := 0

for _, v := range g.supported {
num += len(v)
}

return num
}

func (g *plannedChangeGroups) MappedItemDiffs() []*sdp.MappedItemDiff {
mappedItemDiffs := make([]*sdp.MappedItemDiff, 0)

for _, v := range g.supported {
mappedItemDiffs = append(mappedItemDiffs, v...)
}

for _, v := range g.unsupported {
mappedItemDiffs = append(mappedItemDiffs, v...)
}

return mappedItemDiffs
}

// Add the specified item to the appropriate type group in the supported or unsupported section, based of whether it has a mapping query
func (g *plannedChangeGroups) Add(typ string, item *sdp.MappedItemDiff) {
groups := g.supported
if item.GetMappingQuery() == nil {
groups = g.unsupported
}
list, ok := groups[typ]
if !ok {
list = make([]*sdp.MappedItemDiff, 0)
}
groups[typ] = append(list, item)
}

func changeTitle(arg string) string {
if arg != "" {
// easy, return the user's choice
Expand Down
18 changes: 0 additions & 18 deletions cmd/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,7 @@ type TextStyle struct {
underlying chalk.TextStyle
}

func (t TextStyle) TextStyle(val string) string {
if !tty {
// Don't style if we're not in a TTY
return val
}

return t.underlying.TextStyle(val)
}

// A type that wraps chalk.Color but adds detections for if we're in a TTY
type Color struct {
underlying chalk.Color
}

func (c Color) Color(val string) string {
if !tty {
// Don't style if we're not in a TTY
return val
}

return c.underlying.Color(val)
}
26 changes: 0 additions & 26 deletions cmd/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package cmd
import (
_ "embed"
"fmt"
"strings"

"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glamour/ansi"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap"
)

// constrain the maximum terminal width to avoid readability issues with too
Expand Down Expand Up @@ -320,14 +318,6 @@ func styleH1() lipgloss.Style {
PaddingRight(2)
}

func styleH2() lipgloss.Style {
return lipgloss.NewStyle().
Foreground(ColorPalette.BgMain).
Bold(true).
PaddingLeft(2).
PaddingRight(2)
}

// markdownToString converts the markdown string to a string containing ANSI
// formatting sequences with at most maxWidth visible characters per line. Set
// maxWidth to zero to use the underlying library's default.
Expand All @@ -353,22 +343,6 @@ func markdownToString(maxWidth int, markdown string) string {
return out
}

// wrap ensures that the text is wrapped to the given width and everything but
// the first line is indented by the requested amount. Consider that the current
// implementation is very naive and for large indent values, the first line
// might not be wrapped too early.
//
// Indent is ignored when the requested indent is larger than the current width.
// This is expected to only occur in edge cases, e.g. when the terminal is
// resiyed to very narrow.
func wrap(s string, width, indent int) string {
if indent > width {
indent = 0
}

return strings.ReplaceAll(wordwrap.String(s, width-indent), "\n", "\n"+strings.Repeat(" ", indent))
}

func OkSymbol() string {
if IsConhost() {
return "OK"
Expand Down
67 changes: 0 additions & 67 deletions tfutils/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"regexp"
"strconv"
"strings"

"github.com/xiam/dig"
)

// NOTE: These definitions are copied from the
Expand Down Expand Up @@ -62,71 +60,6 @@ type ConfigModule struct {
Variables variables `json:"variables,omitempty"`
}

// Digs through a map using the same logic that terraform does i.e. foo.bar[0]
func TerraformDig(srcMapPtr interface{}, path string) interface{} {
// Split the path on each period
parts := strings.Split(path, ".")

if len(parts) == 0 {
return nil
}

// Check for an index in this section
indexMatches := indexBrackets.FindStringSubmatch(parts[0])

var value interface{}

if len(indexMatches) == 0 {
// No index, just get the value
value = dig.Interface(srcMapPtr, parts[0])
} else {
// strip the brackets
keyName := indexBrackets.ReplaceAllString(parts[0], "")

// Get the index
index, err := strconv.Atoi(indexMatches[1])

if err != nil {
return nil
}

// Get the value
arr, ok := dig.Interface(srcMapPtr, keyName).([]interface{})

if !ok {
return nil
}

// Check if the index is in range
if index < 0 || index >= len(arr) {
return nil
}

value = arr[index]
}

if len(parts) == 1 {
return value
} else {
// Force it to another map[string]interface{}
valueMap := make(map[string]interface{})

if mapString, ok := value.(map[string]string); ok {
for k, v := range mapString {
valueMap[k] = v
}
} else if mapInterface, ok := value.(map[string]interface{}); ok {
valueMap = mapInterface
} else if mapAttributeValues, ok := value.(AttributeValues); ok {
valueMap = mapAttributeValues
} else {
return nil
}

return TerraformDig(&valueMap, strings.Join(parts[1:], "."))
}
}

var escapeRegex = regexp.MustCompile(`\${([\w\.\[\]]*)}`)

// Digs for a config resource in this module or its children
Expand Down
48 changes: 0 additions & 48 deletions tfutils/plan_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,54 +345,6 @@ func isStateFile(bytes []byte) bool {
return false
}

// Returns the name of the provider from the config key. If the resource isn't
// in a module, the ProviderConfigKey will be something like "kubernetes",
// however if it's in a module it's be something like
// "module.something:kubernetes". In both scenarios we want to return
// "kubernetes"
func extractProviderNameFromConfigKey(providerConfigKey string) string {
sections := strings.Split(providerConfigKey, ":")
return sections[len(sections)-1]
}

// InterpolateScope Will interpolate variables in the scope string. These
// variables can come from the following places:
//
// * `outputs` - These are the outputs from the plan
// * `values` - These are the values from the resource in question
//
// Interpolation is done using the Terraform interpolation syntax:
// https://www.terraform.io/docs/configuration/interpolation.html
func InterpolateScope(scope string, data map[string]any) (string, error) {
// Find all instances of ${} in the Scope
matches := escapeRegex.FindAllStringSubmatch(scope, -1)

interpolated := scope

for _, match := range matches {
// The first match is the entire string, the second match is the
// variable name
variableName := match[1]

value := TerraformDig(&data, variableName)

if value == nil {
return "", fmt.Errorf("variable '%v' not found", variableName)
}

// Convert the value to a string
valueString, ok := value.(string)

if !ok {
return "", fmt.Errorf("variable '%v' is not a string", variableName)
}

interpolated = strings.Replace(interpolated, match[0], valueString, 1)
}

return interpolated, nil
}

func countSensitiveValuesInConfig(m ConfigModule) int {
removedSecrets := 0
for _, v := range m.Variables {
Expand Down
Loading

0 comments on commit 7a9d2a2

Please sign in to comment.