Skip to content

Commit 25315c3

Browse files
committed
fix: force error on apt update
1 parent a7317dc commit 25315c3

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

internal/update/apt/service.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,19 @@ func runDpkgConfigureCommand(ctx context.Context) error {
153153
}
154154

155155
func runUpdateCommand(ctx context.Context) error {
156-
cmd, err := paths.NewProcess(nil, "sudo", "apt-get", "update")
156+
// Create a temporary apt config file for passing custom option,
157+
// without changing the sudoers file.
158+
config, rmConfig, err := getAptUpdateConfigFile()
159+
if err != nil {
160+
slog.Warn("cannot write apt config file, skipped", slog.String("error", err.Error()))
161+
return err
162+
}
163+
defer rmConfig()
164+
165+
cmd, err := paths.NewProcess(
166+
[]string{"APT_CONFIG", config.String()},
167+
"sudo", "apt-get", "update",
168+
)
157169
if err != nil {
158170
return err
159171
}
@@ -163,6 +175,27 @@ func runUpdateCommand(ctx context.Context) error {
163175
return nil
164176
}
165177

178+
func getAptUpdateConfigFile() (*paths.Path, func(), error) {
179+
// Configure apt return error on any warning during update.
180+
configContent := []byte(`APT::Update::Error-Mode "any";`)
181+
182+
config, err := paths.MkTempFile(nil, "apt.config")
183+
if err != nil {
184+
return nil, nil, fmt.Errorf("cannot create apt config file: %w", err)
185+
}
186+
187+
if _, err = config.Write(configContent); err != nil {
188+
return nil, nil, fmt.Errorf("cannot write temporary apt config file: %w", err)
189+
}
190+
191+
path, err := paths.New(config.Name()).Abs()
192+
if err != nil {
193+
return nil, nil, fmt.Errorf("cannot get absolute apt config file path: %w", err)
194+
}
195+
196+
return path, func() { _ = path.Remove() }, nil
197+
}
198+
166199
func runUpgradeCommand(ctx context.Context, names []string) iter.Seq2[string, error] {
167200
env := []string{"NEEDRESTART_MODE=l"}
168201

0 commit comments

Comments
 (0)