MCPcopy Create free account
hub / github.com/nhost/nhost / writeScript

Function writeScript

cli/cmd/dockercredentials/configure.go:65–100  ·  view source on GitHub ↗
(ctx context.Context, ce *clienv.CliEnv)

Source from the content-addressed store, hash-verified

63}
64
65func writeScript(ctx context.Context, ce *clienv.CliEnv) error {
66 ce.Println("Installing credentials helper for docker in %s", credentialsPath)
67
68 executable, err := os.Executable()
69 if err != nil {
70 return fmt.Errorf("could not get executable path: %w", err)
71 }
72
73 script := fmt.Sprintf(script, executable)
74
75 tmpfile, err := os.CreateTemp("", "nhost-docker-credentials")
76 if err != nil {
77 return fmt.Errorf("could not create temporary file: %w", err)
78 }
79 defer tmpfile.Close()
80
81 if _, err := tmpfile.WriteString(script); err != nil {
82 return fmt.Errorf("could not write to temporary file: %w", err)
83 }
84
85 if err := tmpfile.Chmod(0o755); err != nil { //nolint:mnd
86 return fmt.Errorf("could not chmod temporary file: %w", err)
87 }
88
89 if !canSudo(ctx) {
90 ce.Println("I need root privileges to install the file. Please, enter your password.")
91 }
92
93 if err := exec.CommandContext( //nolint:gosec
94 ctx, "sudo", "mv", tmpfile.Name(), credentialsPath,
95 ).Run(); err != nil {
96 return fmt.Errorf("could not move temporary file: %w", err)
97 }
98
99 return nil
100}
101
102func configureDocker(dockerConfig string) error {
103 f, err := os.OpenFile(dockerConfig, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0o644) //nolint:mnd

Callers 1

actionConfigureFunction · 0.85

Calls 7

canSudoFunction · 0.85
PrintlnMethod · 0.80
ErrorfMethod · 0.80
CloseMethod · 0.65
NameMethod · 0.65
WriteStringMethod · 0.45
RunMethod · 0.45

Tested by

no test coverage detected