Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linter issues following linter version upgrade #273

Merged
merged 14 commits into from
Oct 27, 2024
16 changes: 8 additions & 8 deletions cmd/tenv/subcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main
import (
"bytes"
"context"
"errors"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -87,11 +88,10 @@ func newDetectCmd(conf *config.Config, versionManager versionmanager.VersionMana

detectedVersion, err := versionManager.Detect(context.Background(), false)
if err != nil {
if err == versionmanager.ErrNoCompatibleLocally {
loghelper.StdDisplay(err.Error())
} else {
if !errors.Is(err, versionmanager.ErrNoCompatibleLocally) {
return err
}
loghelper.StdDisplay(err.Error())
}
loghelper.StdDisplay(loghelper.Concat(versionManager.FolderName, " ", detectedVersion, " will be run from this directory."))

Expand Down Expand Up @@ -135,7 +135,7 @@ If a parameter is passed, available options:
Long: descBuilder.String(),
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
conf.InitDisplayer(false)

ctx := context.Background()
Expand Down Expand Up @@ -198,12 +198,12 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage
if noUseDate {
loghelper.StdDisplay(loghelper.Concat("* ", version, " (never used, set by ", filePath, ")"))
} else {
loghelper.StdDisplay(loghelper.Concat("* ", version, " (used ", useDate.Format(time.DateOnly), ", set by ", filePath, ")")) //nolint
loghelper.StdDisplay(loghelper.Concat("* ", version, " (used ", useDate.Format(time.DateOnly), ", set by ", filePath, ")"))
}
case noUseDate:
loghelper.StdDisplay(loghelper.Concat(" ", version, " (never used)"))
default:
loghelper.StdDisplay(loghelper.Concat(" ", version, " (used ", useDate.Format(time.DateOnly), ")")) //nolint
loghelper.StdDisplay(loghelper.Concat(" ", version, " (used ", useDate.Format(time.DateOnly), ")"))
}
}
if conf.DisplayVerbose {
Expand Down Expand Up @@ -329,9 +329,9 @@ If a parameter is passed, available parameter options:

if len(args) == 0 {
return uninstallUI(versionManager)
} else {
return versionManager.Uninstall(args[0])
}

return versionManager.Uninstall(args[0])
},
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/tenv/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
tofuHelp = helpPrefix + "OpenTofu (https://opentofu.org)."

pathEnvName = "PATH"

rwPerm = 0o600
)

// can be overridden with ldflags.
Expand Down Expand Up @@ -159,7 +161,7 @@ func manageNoArgsCmd(conf *config.Config, hclParser *hclparse.Parser) {

ctx := context.Background()
if err := toolUI(ctx, conf, hclParser); err != nil {
fmt.Println(err.Error())
fmt.Println(err.Error()) //nolint

os.Exit(1)
}
Expand Down Expand Up @@ -210,7 +212,7 @@ func newUpdatePathCmd(gha bool) *cobra.Command {
if gha {
pathfilePath := os.Getenv("GITHUB_PATH")
if pathfilePath != "" {
pathfile, err := os.OpenFile(pathfilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
pathfile, err := os.OpenFile(pathfilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, rwPerm)
if err != nil {
return err
}
Expand Down
118 changes: 59 additions & 59 deletions cmd/tenv/textui.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const (
selectedColorCode = "170"
)

var tools = []list.Item{item(cmdconst.TofuName), item(cmdconst.TerraformName), item(cmdconst.TerragruntName), item(cmdconst.AtmosName)}
var tools = []list.Item{item(cmdconst.TofuName), item(cmdconst.TerraformName), item(cmdconst.TerragruntName), item(cmdconst.AtmosName)} //nolint

var (
helpStyle = list.DefaultStyles().HelpStyle
paginationStyle = list.DefaultStyles().PaginationStyle
selectedItemStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(selectedColorCode))
titleStyle = lipgloss.NewStyle()
helpStyle = list.DefaultStyles().HelpStyle //nolint
paginationStyle = list.DefaultStyles().PaginationStyle //nolint
selectedItemStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(selectedColorCode)) //nolint
titleStyle = lipgloss.NewStyle() //nolint
)

type item string
Expand All @@ -66,18 +66,18 @@ type itemDelegate struct {
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
func (d itemDelegate) Render(writer io.Writer, displayList list.Model, index int, listItem list.Item) {
version, selected := listItem.FilterValue(), " "
if _, ok := d.choices[version]; ok {
selected = "X"
}
line := loghelper.Concat("[", selected, "] ", version)

if index == m.Index() {
if index == displayList.Index() {
line = selectedItemStyle.Render(line)
}

fmt.Fprint(w, line)
fmt.Fprint(writer, line)
}

type manageItemDelegate struct {
Expand All @@ -88,7 +88,7 @@ type manageItemDelegate struct {
func (d manageItemDelegate) Height() int { return 1 }
func (d manageItemDelegate) Spacing() int { return 0 }
func (d manageItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d manageItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
func (d manageItemDelegate) Render(writer io.Writer, displayList list.Model, index int, listItem list.Item) {
version, selectedStr := listItem.FilterValue(), " "
_, selected := d.choices[version]
_, installed := d.installed[version]
Expand All @@ -107,24 +107,24 @@ func (d manageItemDelegate) Render(w io.Writer, m list.Model, index int, listIte

line := loghelper.Concat("[", selectedStr, "] ", version)

if index == m.Index() {
if index == displayList.Index() {
line = selectedItemStyle.Render(line)
}

fmt.Fprint(w, line)
fmt.Fprint(writer, line)
}

type itemModel struct {
type itemSelector struct {
choices map[string]struct{}
list list.Model
quitting bool
}

func (m itemModel) Init() tea.Cmd {
func (m itemSelector) Init() tea.Cmd {
return nil
}

func (m itemModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m itemSelector) Update(msg tea.Msg) (tea.Model, tea.Cmd) { //nolint
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (m itemModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m itemModel) View() string {
func (m itemSelector) View() string {
if m.quitting {
return ""
}
Expand All @@ -183,28 +183,28 @@ func toolUI(ctx context.Context, conf *config.Config, hclParser *hclparse.Parser
choices: selection,
}

l := list.New(tools, delegate, defaultWidth, listHeight)
l.Title = "Which tool do you want to manage ?"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
displayList := list.New(tools, delegate, defaultWidth, listHeight)
displayList.Title = "Which tool do you want to manage ?"
displayList.SetShowStatusBar(false)
displayList.SetFilteringEnabled(false)
displayList.Styles.Title = titleStyle
displayList.Styles.PaginationStyle = paginationStyle
displayList.Styles.HelpStyle = helpStyle

l.AdditionalFullHelpKeys = additionalFullHelpKeys
l.AdditionalShortHelpKeys = additionalShortHelpKeys
displayList.AdditionalFullHelpKeys = additionalFullHelpKeys
displayList.AdditionalShortHelpKeys = additionalShortHelpKeys

m := itemModel{
selector := itemSelector{
choices: selection,
list: l,
list: displayList,
}

_, err := tea.NewProgram(m).Run()
_, err := tea.NewProgram(selector).Run()
if err != nil {
return err
}

if len(m.choices) == 0 {
if len(selector.choices) == 0 {
loghelper.StdDisplay("No selected tool")

return nil
Expand Down Expand Up @@ -243,36 +243,36 @@ func manageUI(ctx context.Context, versionManager versionmanager.VersionManager)
installed: installed,
}

l := list.New(items, delegate, defaultWidth, listHeight)
l.Title = loghelper.Concat("Which ", versionManager.FolderName, " version(s) do you want to install(I) or uninstall(U) ? (X mark already installed)")
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
displayList := list.New(items, delegate, defaultWidth, listHeight)
displayList.Title = loghelper.Concat("Which ", versionManager.FolderName, " version(s) do you want to install(I) or uninstall(U) ? (X mark already installed)")
displayList.SetShowStatusBar(false)
displayList.SetFilteringEnabled(false)
displayList.Styles.Title = titleStyle
displayList.Styles.PaginationStyle = paginationStyle
displayList.Styles.HelpStyle = helpStyle

l.AdditionalFullHelpKeys = additionalFullHelpKeys
l.AdditionalShortHelpKeys = additionalShortHelpKeys
displayList.AdditionalFullHelpKeys = additionalFullHelpKeys
displayList.AdditionalShortHelpKeys = additionalShortHelpKeys

m := itemModel{
selector := itemSelector{
choices: selection,
list: l,
list: displayList,
}

_, err = tea.NewProgram(m).Run()
_, err = tea.NewProgram(selector).Run()
if err != nil {
return err
}

if len(m.choices) == 0 {
if len(selector.choices) == 0 {
loghelper.StdDisplay(loghelper.Concat("No selected ", versionManager.FolderName, " versions"))

return nil
}

toInstall := make([]string, 0, len(m.choices))
toUninstall := make([]string, 0, len(m.choices))
for version := range m.choices {
toInstall := make([]string, 0, len(selector.choices))
toUninstall := make([]string, 0, len(selector.choices))
for version := range selector.choices {
if _, installed := installed[version]; installed {
toUninstall = append(toUninstall, version)
} else {
Expand Down Expand Up @@ -307,35 +307,35 @@ func uninstallUI(versionManager versionmanager.VersionManager) error {
choices: selection,
}

l := list.New(items, delegate, defaultWidth, listHeight)
l.Title = loghelper.Concat("Which ", versionManager.FolderName, " version(s) do you want to uninstall ?")
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
displayList := list.New(items, delegate, defaultWidth, listHeight)
displayList.Title = loghelper.Concat("Which ", versionManager.FolderName, " version(s) do you want to uninstall ?")
displayList.SetShowStatusBar(false)
displayList.SetFilteringEnabled(false)
displayList.Styles.Title = titleStyle
displayList.Styles.PaginationStyle = paginationStyle
displayList.Styles.HelpStyle = helpStyle

l.AdditionalFullHelpKeys = additionalFullHelpKeys
l.AdditionalShortHelpKeys = additionalShortHelpKeys
displayList.AdditionalFullHelpKeys = additionalFullHelpKeys
displayList.AdditionalShortHelpKeys = additionalShortHelpKeys

m := itemModel{
selector := itemSelector{
choices: selection,
list: l,
list: displayList,
}

_, err = tea.NewProgram(m).Run()
_, err = tea.NewProgram(selector).Run()
if err != nil {
return err
}

if len(m.choices) == 0 {
if len(selector.choices) == 0 {
loghelper.StdDisplay(loghelper.Concat("No selected ", versionManager.FolderName, " versions"))

return nil
}

selected := make([]string, 0, len(m.choices))
for version := range m.choices {
selected := make([]string, 0, len(selector.choices))
for version := range selector.choices {
selected = append(selected, version)
}
slices.SortFunc(selected, semantic.CmpVersion)
Expand Down
4 changes: 3 additions & 1 deletion pkg/check/cosign/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
const (
cosignExecName = "cosign"
verified = "Verified OK"

rwPerm = 0o600
)

var (
Expand Down Expand Up @@ -93,7 +95,7 @@ func tempFile(name string, data []byte) (string, func(), error) {
}

tmpFileName := tmpFile.Name()
if err = os.WriteFile(tmpFileName, data, 0o600); err != nil {
if err = os.WriteFile(tmpFileName, data, rwPerm); err != nil {
return "", nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/check/sha256/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Check(data []byte, dataSums []byte, fileName string) error {
func extract(dataSums []byte, fileName string) ([]byte, error) {
dataSumsStr := string(dataSums)
for _, dataSumStr := range strings.Split(dataSumsStr, "\n") {
dataSumStr, ok := strings.CutSuffix(dataSumStr, fileName) //nolint
dataSumStr, ok := strings.CutSuffix(dataSumStr, fileName)
if ok {
dataSumStr = strings.TrimSpace(dataSumStr)

Expand Down
5 changes: 3 additions & 2 deletions pkg/check/sha256/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package sha256check_test

import (
_ "embed"
"errors"
"testing"

sha256check "github.com/tofuutils/tenv/v3/pkg/check/sha256"
Expand All @@ -44,7 +45,7 @@ func TestSha256CheckError(t *testing.T) {

if err := sha256check.Check(data, dataSums, "hello2.txt"); err == nil {
t.Error("Should fail on non corresponding file and fileName")
} else if err != sha256check.ErrCheck {
} else if !errors.Is(err, sha256check.ErrCheck) {
t.Error("Incorrect error reported, get :", err)
}
}
Expand All @@ -54,7 +55,7 @@ func TestSha256Extract(t *testing.T) {

if err := sha256check.Check(data, dataSums, "any_name.txt"); err == nil {
t.Error("Should fail on non exiting fileName")
} else if err != sha256check.ErrNoSum {
} else if !errors.Is(err, sha256check.ErrNoSum) {
t.Error("Incorrect error reported, get :", err)
}
}
4 changes: 3 additions & 1 deletion pkg/cmdproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"strings"
)

const rwPerm = 0o600

var errDelimiter = errors.New("key and value should not contains delimiter")

// Always call os.Exit.
Expand Down Expand Up @@ -81,7 +83,7 @@ func initIO(cmd *exec.Cmd, pExitCode *int, gha bool) func() {
}

outputPath := os.Getenv("GITHUB_OUTPUT")
outputFile, err := os.OpenFile(outputPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) //nolint
outputFile, err := os.OpenFile(outputPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, rwPerm)
if err != nil {
fmt.Println("Ignore GITHUB_ACTIONS, fail to open GITHUB_OUTPUT :", err) //nolint

Expand Down
Loading
Loading