Skip to content

Commit 6daefdc

Browse files
authored
feat(mock): quick way to add packages (#417)
1 parent c045f5c commit 6daefdc

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

internal/app/azldev/cmds/advanced/mock.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type ShellOptions struct {
7878

7979
// Whether or not to enable external network access from within the mock root the shell is executed in.
8080
EnableNetwork bool
81+
82+
// Packages to add to the mock root before starting the shell.
83+
PackagesToAdd []string
8184
}
8285

8386
// Constructs a [cobra.Command] for the 'mock shell' command.
@@ -97,6 +100,9 @@ func NewShellCmd() *cobra.Command {
97100

98101
cmd.Flags().StringVarP(&options.MockConfigPath, "config", "c", "", "Path to the mock .cfg file")
99102
cmd.Flags().BoolVar(&options.EnableNetwork, "enable-network", false, "Enable network access in the mock root")
103+
cmd.Flags().StringArrayVarP(&options.PackagesToAdd, "add-package", "p", []string{},
104+
"Package to add to the mock root before starting the shell",
105+
)
100106

101107
return cmd
102108
}
@@ -135,6 +141,13 @@ func RunShell(env *azldev.Env, options *ShellOptions, extraArgs []string) error
135141
runner.EnableNetwork()
136142
}
137143

144+
if len(options.PackagesToAdd) > 0 {
145+
err = runner.InstallPackages(env, options.PackagesToAdd)
146+
if err != nil {
147+
return fmt.Errorf("failed to install packages in mock root (%v): %w", options.PackagesToAdd, err)
148+
}
149+
}
150+
138151
cmd, err := runner.CmdInChroot(env, extraArgs, true /*interactive*/)
139152
if err != nil {
140153
return fmt.Errorf("failed to create shell command: %w", err)

internal/rpm/mock/mock.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,36 @@ func (r *Runner) CmdInChroot(ctx context.Context, args []string, interactive boo
507507
return cmd, nil
508508
}
509509

510+
// InstallPackages installs the specified packages into the mock root.
511+
func (r *Runner) InstallPackages(ctx context.Context, packages []string) error {
512+
err := r.ensureMockPresentAndConfigured()
513+
if err != nil {
514+
return err
515+
}
516+
517+
mockArgs := r.getBaseArgs()
518+
mockArgs = append(mockArgs, "--install")
519+
mockArgs = append(mockArgs, packages...)
520+
521+
cmd := exec.CommandContext(ctx, MockBinary, mockArgs...)
522+
cmd.Stdout = os.Stdout
523+
cmd.Stderr = os.Stderr
524+
525+
extcmd, err := r.cmdFactory.Command(cmd)
526+
if err != nil {
527+
return fmt.Errorf("failed to create external command for mock:\n%w", err)
528+
}
529+
530+
extcmd = extcmd.SetLongRunning("Waiting for mock (installing packages)...")
531+
532+
err = extcmd.Run(ctx)
533+
if err != nil {
534+
return fmt.Errorf("mock failed to install packages (%v):\n%w", packages, err)
535+
}
536+
537+
return nil
538+
}
539+
510540
// ScrubRoot removes the mock root.
511541
func (r *Runner) ScrubRoot(ctx context.Context) error {
512542
err := r.ensureMockPresentAndConfigured()

0 commit comments

Comments
 (0)