mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: format Go more aggressively
This commit is contained in:
@@ -78,7 +78,7 @@ func extractTar(t *testing.T, data []byte, directory string) {
|
||||
path := filepath.Join(directory, header.Name)
|
||||
mode := header.FileInfo().Mode()
|
||||
if mode == 0 {
|
||||
mode = 0600
|
||||
mode = 0o600
|
||||
}
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
|
||||
+2
-2
@@ -60,7 +60,7 @@ func (f File) Delete() error {
|
||||
|
||||
// Write writes the string to the file.
|
||||
func (f File) Write(s string) error {
|
||||
return write(string(f), 0600, []byte(s))
|
||||
return write(string(f), 0o600, []byte(s))
|
||||
}
|
||||
|
||||
// Read reads the file to a string.
|
||||
@@ -72,7 +72,7 @@ func (f File) Read() (string, error) {
|
||||
// open opens a file in the configuration directory,
|
||||
// creating all intermediate directories.
|
||||
func open(path string, flag int, mode os.FileMode) (*os.File, error) {
|
||||
err := os.MkdirAll(filepath.Dir(path), 0750)
|
||||
err := os.MkdirAll(filepath.Dir(path), 0o750)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+4
-2
@@ -351,7 +351,8 @@ func TestCreateWithRichParameters(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
ProvisionApply: []*proto.Provision_Response{{
|
||||
Type: &proto.Provision_Response_Complete{
|
||||
Complete: &proto.Provision_Complete{},
|
||||
@@ -475,7 +476,8 @@ func TestCreateValidateRichParameters(t *testing.T) {
|
||||
Parameters: richParameters,
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
ProvisionApply: []*proto.Provision_Response{
|
||||
{
|
||||
Type: &proto.Provision_Response_Complete{
|
||||
|
||||
+1
-1
@@ -111,7 +111,7 @@ func dotfiles() *cobra.Command {
|
||||
}
|
||||
|
||||
// ensure command dir exists
|
||||
err = os.MkdirAll(gitCmdDir, 0750)
|
||||
err = os.MkdirAll(gitCmdDir, 0o750)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("ensuring dir at %s: %w", gitCmdDir, err)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestDotfiles(t *testing.T) {
|
||||
testRepo := testGitRepo(t, root)
|
||||
|
||||
// nolint:gosec
|
||||
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0750)
|
||||
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
|
||||
require.NoError(t, err)
|
||||
|
||||
c := exec.Command("git", "add", ".bashrc")
|
||||
@@ -56,7 +56,7 @@ func TestDotfiles(t *testing.T) {
|
||||
testRepo := testGitRepo(t, root)
|
||||
|
||||
// nolint:gosec
|
||||
err := os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0750)
|
||||
err := os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0o750)
|
||||
require.NoError(t, err)
|
||||
|
||||
c := exec.Command("git", "add", "install.sh")
|
||||
@@ -82,12 +82,12 @@ func TestDotfiles(t *testing.T) {
|
||||
testRepo := testGitRepo(t, root)
|
||||
|
||||
// nolint:gosec
|
||||
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0750)
|
||||
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
|
||||
require.NoError(t, err)
|
||||
|
||||
// add a conflicting file at destination
|
||||
// nolint:gosec
|
||||
err = os.WriteFile(filepath.Join(string(root), ".bashrc"), []byte("backup"), 0750)
|
||||
err = os.WriteFile(filepath.Join(string(root), ".bashrc"), []byte("backup"), 0o750)
|
||||
require.NoError(t, err)
|
||||
|
||||
c := exec.Command("git", "add", ".bashrc")
|
||||
@@ -119,7 +119,7 @@ func testGitRepo(t *testing.T, root config.Root) string {
|
||||
r, err := cryptorand.String(8)
|
||||
require.NoError(t, err)
|
||||
dir := filepath.Join(string(root), fmt.Sprintf("test-repo-%s", r))
|
||||
err = os.MkdirAll(dir, 0750)
|
||||
err = os.MkdirAll(dir, 0o750)
|
||||
require.NoError(t, err)
|
||||
|
||||
c := exec.Command("git", "init")
|
||||
|
||||
+1
-1
@@ -149,7 +149,7 @@ func TestLogout(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
// Changing the permissions to throw error during deletion.
|
||||
err = os.Chmod(string(config), 0500)
|
||||
err = os.Chmod(string(config), 0o500)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
defer func() {
|
||||
|
||||
@@ -18,7 +18,6 @@ func createParameterMapFromFile(parameterFile string) (map[string]string, error)
|
||||
parameterMap := make(map[string]string)
|
||||
|
||||
parameterFileContents, err := os.ReadFile(parameterFile)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+1
-3
@@ -11,9 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func publickey() *cobra.Command {
|
||||
var (
|
||||
reset bool
|
||||
)
|
||||
var reset bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "publickey",
|
||||
|
||||
@@ -15,9 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func resetPassword() *cobra.Command {
|
||||
var (
|
||||
postgresURL string
|
||||
)
|
||||
var postgresURL string
|
||||
|
||||
root := &cobra.Command{
|
||||
Use: "reset-password <username>",
|
||||
|
||||
+1
-3
@@ -64,9 +64,7 @@ const (
|
||||
envURL = "CODER_URL"
|
||||
)
|
||||
|
||||
var (
|
||||
errUnauthenticated = xerrors.New(notLoggedInMessage)
|
||||
)
|
||||
var errUnauthenticated = xerrors.New(notLoggedInMessage)
|
||||
|
||||
func init() {
|
||||
// Set cobra template functions in init to avoid conflicts in tests.
|
||||
|
||||
+5
-5
@@ -309,9 +309,7 @@ func (r *userCleanupRunner) Run(ctx context.Context, _ string, _ io.Writer) erro
|
||||
}
|
||||
|
||||
func scaletestCleanup() *cobra.Command {
|
||||
var (
|
||||
cleanupStrategy = &scaletestStrategyFlags{cleanup: true}
|
||||
)
|
||||
cleanupStrategy := &scaletestStrategyFlags{cleanup: true}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "cleanup",
|
||||
@@ -810,8 +808,10 @@ type runnableTraceWrapper struct {
|
||||
span trace.Span
|
||||
}
|
||||
|
||||
var _ harness.Runnable = &runnableTraceWrapper{}
|
||||
var _ harness.Cleanable = &runnableTraceWrapper{}
|
||||
var (
|
||||
_ harness.Runnable = &runnableTraceWrapper{}
|
||||
_ harness.Cleanable = &runnableTraceWrapper{}
|
||||
)
|
||||
|
||||
func (r *runnableTraceWrapper) Run(ctx context.Context, id string, logs io.Writer) error {
|
||||
ctx, span := r.tracer.Start(ctx, r.spanName, trace.WithNewRoot())
|
||||
|
||||
+2
-4
@@ -976,9 +976,7 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
|
||||
|
||||
// parseURL parses a string into a URL.
|
||||
func parseURL(u string) (*url.URL, error) {
|
||||
var (
|
||||
hasScheme = strings.HasPrefix(u, "http:") || strings.HasPrefix(u, "https:")
|
||||
)
|
||||
hasScheme := strings.HasPrefix(u, "http:") || strings.HasPrefix(u, "https:")
|
||||
|
||||
if !hasScheme {
|
||||
return nil, xerrors.Errorf("URL %q must have a scheme of either http or https", u)
|
||||
@@ -1529,7 +1527,7 @@ func buildLogger(cmd *cobra.Command, cfg *codersdk.DeploymentConfig) (slog.Logge
|
||||
sinks = append(sinks, sinkFn(cmd.ErrOrStderr()))
|
||||
|
||||
default:
|
||||
fi, err := os.OpenFile(loc, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
||||
fi, err := os.OpenFile(loc, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("open log file %q: %w", loc, err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ func statePull() *cobra.Command {
|
||||
return nil
|
||||
}
|
||||
|
||||
return os.WriteFile(args[1], state, 0600)
|
||||
return os.WriteFile(args[1], state, 0o600)
|
||||
},
|
||||
}
|
||||
cmd.Flags().IntVarP(&buildNumber, "build", "b", 0, "Specify a workspace build to target by name.")
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@ func templateInit() *cobra.Command {
|
||||
relPath = "./" + relPath
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Extracting %s to %s...\n", cliui.Styles.Field.Render(selectedTemplate.ID), relPath)
|
||||
err = os.MkdirAll(directory, 0700)
|
||||
err = os.MkdirAll(directory, 0o700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func TestTemplateList(t *testing.T) {
|
||||
}()
|
||||
|
||||
// expect that templates are listed alphabetically
|
||||
var templatesList = []string{firstTemplate.Name, secondTemplate.Name}
|
||||
templatesList := []string{firstTemplate.Name, secondTemplate.Name}
|
||||
sort.Strings(templatesList)
|
||||
|
||||
require.NoError(t, <-errC)
|
||||
|
||||
+1
-1
@@ -108,7 +108,7 @@ func templatePull() *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
err = os.WriteFile(dest, raw, 0600)
|
||||
err = os.WriteFile(dest, raw, 0o600)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("write to path: %w", err)
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestTemplatePull(t *testing.T) {
|
||||
|
||||
// Create the file so that we can test that the command
|
||||
// warns the user before overwriting a preexisting file.
|
||||
fi, err := os.OpenFile(dest, os.O_CREATE|os.O_RDONLY, 0600)
|
||||
fi, err := os.OpenFile(dest, os.O_CREATE|os.O_RDONLY, 0o600)
|
||||
require.NoError(t, err)
|
||||
_ = fi.Close()
|
||||
|
||||
|
||||
+1
-3
@@ -34,9 +34,7 @@ func (pf *templateUploadFlags) stdin() bool {
|
||||
}
|
||||
|
||||
func (pf *templateUploadFlags) upload(cmd *cobra.Command, client *codersdk.Client) (*codersdk.UploadResponse, error) {
|
||||
var (
|
||||
content io.Reader
|
||||
)
|
||||
var content io.Reader
|
||||
if pf.stdin() {
|
||||
content = cmd.InOrStdin()
|
||||
} else {
|
||||
|
||||
@@ -99,7 +99,7 @@ type templateVersionRow struct {
|
||||
func templateVersionsToRows(activeVersionID uuid.UUID, templateVersions ...codersdk.TemplateVersion) []templateVersionRow {
|
||||
rows := make([]templateVersionRow, len(templateVersions))
|
||||
for i, templateVersion := range templateVersions {
|
||||
var activeStatus = ""
|
||||
activeStatus := ""
|
||||
if templateVersion.ID == activeVersionID {
|
||||
activeStatus = cliui.Styles.Code.Render(cliui.Styles.Keyword.Render("Active"))
|
||||
}
|
||||
|
||||
+1
-3
@@ -47,9 +47,7 @@ func tokens() *cobra.Command {
|
||||
}
|
||||
|
||||
func createToken() *cobra.Command {
|
||||
var (
|
||||
tokenLifetime time.Duration
|
||||
)
|
||||
var tokenLifetime time.Duration
|
||||
cmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a tokens",
|
||||
|
||||
+4
-2
@@ -172,7 +172,8 @@ func TestUpdateWithRichParameters(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
ProvisionApply: []*proto.Provision_Response{{
|
||||
Type: &proto.Provision_Response_Complete{
|
||||
Complete: &proto.Provision_Complete{},
|
||||
@@ -269,7 +270,8 @@ func TestUpdateValidateRichParameters(t *testing.T) {
|
||||
Parameters: richParameters,
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
ProvisionApply: []*proto.Provision_Response{
|
||||
{
|
||||
Type: &proto.Provision_Response_Complete{
|
||||
|
||||
+5
-3
@@ -12,9 +12,11 @@ import (
|
||||
"github.com/coder/coder/coderd/util/tz"
|
||||
)
|
||||
|
||||
var errInvalidScheduleFormat = xerrors.New("Schedule must be in the format Mon-Fri 09:00AM America/Chicago")
|
||||
var errInvalidTimeFormat = xerrors.New("Start time must be in the format hh:mm[am|pm] or HH:MM")
|
||||
var errUnsupportedTimezone = xerrors.New("The location you provided looks like a timezone. Check https://ipinfo.io for your location.")
|
||||
var (
|
||||
errInvalidScheduleFormat = xerrors.New("Schedule must be in the format Mon-Fri 09:00AM America/Chicago")
|
||||
errInvalidTimeFormat = xerrors.New("Start time must be in the format hh:mm[am|pm] or HH:MM")
|
||||
errUnsupportedTimezone = xerrors.New("The location you provided looks like a timezone. Check https://ipinfo.io for your location.")
|
||||
)
|
||||
|
||||
// durationDisplay formats a duration for easier display:
|
||||
// - Durations of 24 hours or greater are displays as Xd
|
||||
|
||||
+2
-2
@@ -74,7 +74,7 @@ func vscodeSSH() *cobra.Command {
|
||||
ctx, cancel := context.WithCancel(cmd.Context())
|
||||
defer cancel()
|
||||
|
||||
err = fs.MkdirAll(networkInfoDir, 0700)
|
||||
err = fs.MkdirAll(networkInfoDir, 0o700)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("mkdir: %w", err)
|
||||
}
|
||||
@@ -168,7 +168,7 @@ func vscodeSSH() *cobra.Command {
|
||||
sendErr(err)
|
||||
return
|
||||
}
|
||||
err = afero.WriteFile(fs, networkInfoFilePath, rawStats, 0600)
|
||||
err = afero.WriteFile(fs, networkInfoFilePath, rawStats, 0o600)
|
||||
if err != nil {
|
||||
sendErr(err)
|
||||
return
|
||||
|
||||
@@ -41,9 +41,9 @@ func TestVSCodeSSH(t *testing.T) {
|
||||
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
|
||||
|
||||
fs := afero.NewMemMapFs()
|
||||
err = afero.WriteFile(fs, "/url", []byte(client.URL.String()), 0600)
|
||||
err = afero.WriteFile(fs, "/url", []byte(client.URL.String()), 0o600)
|
||||
require.NoError(t, err)
|
||||
err = afero.WriteFile(fs, "/token", []byte(client.SessionToken()), 0600)
|
||||
err = afero.WriteFile(fs, "/token", []byte(client.SessionToken()), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
cmd, _ := clitest.New(t,
|
||||
|
||||
Reference in New Issue
Block a user