InitWithPlatform initializes celer with platform.
(platform string, opts InitOption)
| 128 | |
| 129 | // InitWithPlatform initializes celer with platform. |
| 130 | func (c *Celer) InitWithPlatform(platform string, opts InitOption) error { |
| 131 | c.platform.ctx = c |
| 132 | |
| 133 | configPath := filepath.Join(dirs.WorkspaceDir, "celer.toml") |
| 134 | if !fileio.PathExists(configPath) { |
| 135 | // Create conf dir if not exists. |
| 136 | if err := os.MkdirAll(filepath.Dir(configPath), os.ModePerm); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | |
| 140 | // Use all CPU cores for CI, otherwise use all cores except 1 to avoid blocking UI. |
| 141 | var jobs int |
| 142 | if _, ok := os.LookupEnv("GITHUB_ACTIONS"); ok { |
| 143 | jobs = runtime.NumCPU() |
| 144 | fmt.Printf("-- GITHUB_ACTIONS: jobs: %d\n", jobs) |
| 145 | } else { |
| 146 | jobs = runtime.NumCPU() - 1 |
| 147 | } |
| 148 | |
| 149 | // Default global values. |
| 150 | c.Main = main{ |
| 151 | BuildType: "release", |
| 152 | Downloads: "", |
| 153 | Jobs: jobs, |
| 154 | Offline: false, |
| 155 | Verbose: false, |
| 156 | } |
| 157 | |
| 158 | // Create celer conf file with default values. |
| 159 | bytes, err := toml.Marshal(c) |
| 160 | if err != nil { |
| 161 | return fmt.Errorf("failed to marshal conf -> %w", err) |
| 162 | } |
| 163 | |
| 164 | // Set platform and init platform if specified. |
| 165 | if platform != "" { |
| 166 | c.Main.Platform = platform |
| 167 | if err := c.platform.Init(c.Main.Platform); err != nil { |
| 168 | // Skip platform init if platform not exist. |
| 169 | if errors.Is(err, errors.ErrPlatformNotExist) && opts.SkipPlatform { |
| 170 | c.Main.Platform = "" |
| 171 | return nil |
| 172 | } |
| 173 | return err |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if err := os.WriteFile(configPath, bytes, os.ModePerm); err != nil { |
| 178 | return err |
| 179 | } |
| 180 | } else { |
| 181 | // Read celer conf. |
| 182 | bytes, err := os.ReadFile(configPath) |
| 183 | if err != nil { |
| 184 | return fmt.Errorf("failed to read conf -> %w", err) |
| 185 | } |
| 186 | if err := toml.Unmarshal(bytes, c); err != nil { |
| 187 | return fmt.Errorf("failed to unmarshal conf -> %w", err) |