跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

restart.go

130 行 · 3.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
7 "bytes"
8 "fmt"
9 "os"
10 "path/filepath"
11
12 "github.com/sirupsen/logrus"
13 "github.com/spf13/cobra"
14
15 "github.com/lima-vm/lima/v2/cmd/limactl/editflags"
16 "github.com/lima-vm/lima/v2/pkg/driverutil"
17 "github.com/lima-vm/lima/v2/pkg/instance"
18 "github.com/lima-vm/lima/v2/pkg/limatype"
19 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
20 "github.com/lima-vm/lima/v2/pkg/limayaml"
21 "github.com/lima-vm/lima/v2/pkg/store"
22 "github.com/lima-vm/lima/v2/pkg/yqutil"
23)
24
25func newRestartCommand() *cobra.Command {
26 restartCmd := &cobra.Command{
27 Use: "restart INSTANCE",
28 Short: "Restart a running instance",
29 Args: WrapArgsError(cobra.MaximumNArgs(1)),
30 RunE: restartAction,
31 ValidArgsFunction: restartBashComplete,
32 GroupID: basicCommand,
33 }
34
35 restartCmd.Flags().BoolP("force", "f", false, "Force stop and restart the instance")
36 restartCmd.Flags().Bool("progress", false, "Show provision script progress by tailing cloud-init logs")
37 editflags.RegisterEdit(restartCmd, "")
38 return restartCmd
39}
40
41func restartAction(cmd *cobra.Command, args []string) error {
42 ctx := cmd.Context()
43 instName := DefaultInstanceName
44 if len(args) > 0 {
45 instName = args[0]
46 }
47
48 inst, err := store.Inspect(ctx, instName)
49 if err != nil {
50 return err
51 }
52
53 flags := cmd.Flags()
54 force, err := flags.GetBool("force")
55 if err != nil {
56 return err
57 }
58 progress, err := flags.GetBool("progress")
59 if err != nil {
60 return err
61 }
62
63 filePath := filepath.Join(inst.Dir, filenames.LimaYAML)
64 yContent, err := os.ReadFile(filePath)
65 if err != nil {
66 return err
67 }
68
69 var params map[string]string
70 if flags.Changed("param") {
71 var y limatype.LimaYAML
72 if err := limayaml.Unmarshal(yContent, &y, filePath); err != nil {
73 return err
74 }
75 params = y.Param
76 }
77
78 yqExprs, err := editflags.YQExpressions(flags, false, params)
79 if err != nil {
80 return err
81 }
82
83 if len(yqExprs) > 0 {
84 yq := yqutil.Join(yqExprs)
85 yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
86 if err != nil {
87 return err
88 }
89 if !bytes.Equal(yBytes, yContent) {
90 y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
91 if err != nil {
92 return err
93 }
94 if err := driverutil.ResolveVMType(y); err != nil {
95 return fmt.Errorf("failed to resolve vm for %#q: %w", filePath, err)
96 }
97 inst.Config = y
98 limaDriver, err := driverutil.CreateConfiguredDriver(ctx, inst, 0)
99 if err != nil {
100 return err
101 }
102 if err := limaDriver.Validate(ctx); err != nil {
103 return saveRejectedYAML(yBytes, err)
104 }
105 if err := limayaml.Validate(inst.Config, true); err != nil {
106 return saveRejectedYAML(yBytes, err)
107 }
108 if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
109 return saveRejectedYAML(yBytes, err)
110 }
111 if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
112 return err
113 }
114 logrus.Infof("Instance %#q configuration edited", inst.Name)
115 inst, err = store.Inspect(ctx, instName)
116 if err != nil {
117 return err
118 }
119 }
120 }
121
122 if force {
123 return instance.RestartForcibly(ctx, inst, progress)
124 }
125 return instance.Restart(ctx, inst, progress)
126}
127
128func restartBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
129 return bashCompleteInstanceNames(cmd)
130}