forked from alexellis/arkade
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>
- Loading branch information
1 parent
d3bdc46
commit 8772b96
Showing
3 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) arkade author(s) 2024. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
package system | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/alexellis/arkade/pkg/archive" | ||
"github.com/alexellis/arkade/pkg/env" | ||
"github.com/alexellis/arkade/pkg/get" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func MakeInstallCaddyServer() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "caddy", | ||
Short: "Install Caddy Server", | ||
Long: `Install Caddy Server which is an extensible server platform that uses TLS by default`, | ||
Example: ` arkade system install caddy | ||
arkade system install caddy --version <version>`, | ||
SilenceUsage: true, | ||
} | ||
|
||
command.Flags().StringP("version", "v", "", "The version or leave blank to determine the latest available version") | ||
command.Flags().String("path", "/usr/local/bin", "Installation path, where a caddy subfolder will be created") | ||
command.Flags().Bool("progress", true, "Show download progress") | ||
command.Flags().String("arch", "", "CPU architecture i.e. amd64") | ||
|
||
command.PreRunE = func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
} | ||
|
||
command.RunE = func(cmd *cobra.Command, args []string) error { | ||
installPath, _ := cmd.Flags().GetString("path") | ||
version, _ := cmd.Flags().GetString("version") | ||
fmt.Printf("Installing Caddy Server to %s\n", installPath) | ||
|
||
installPath = strings.ReplaceAll(installPath, "$HOME", os.Getenv("HOME")) | ||
|
||
if err := os.MkdirAll(installPath, 0755); err != nil && !os.IsExist(err) { | ||
fmt.Printf("Error creating directory %s, error: %s\n", installPath, err.Error()) | ||
} | ||
|
||
arch, osVer := env.GetClientArch() | ||
|
||
if strings.ToLower(osVer) != "linux" { | ||
return fmt.Errorf("this app only supports Linux") | ||
} | ||
|
||
if cmd.Flags().Changed("arch") { | ||
arch, _ = cmd.Flags().GetString("arch") | ||
} | ||
|
||
dlArch := arch | ||
if arch == "x86_64" { | ||
dlArch = "amd64" | ||
} else if arch == "aarch64" { | ||
dlArch = "arm64" | ||
} else { | ||
dlArch = arch | ||
} | ||
|
||
if version == "" { | ||
v, err := get.FindGitHubRelease("caddyserver", "caddy") | ||
if err != nil { | ||
return err | ||
} | ||
version = v | ||
} else if !strings.HasPrefix(version, "v") { | ||
version = "v" + version | ||
} | ||
|
||
fmt.Printf("Installing version: %s for: %s\n", version, dlArch) | ||
|
||
filename := fmt.Sprintf("caddy_%s_linux_%s.tar.gz", strings.TrimPrefix(version, "v"), dlArch) | ||
dlURL := fmt.Sprintf(githubDownloadTemplate, "caddyserver", "caddy", version, filename) | ||
|
||
fmt.Printf("Downloading from: %s\n", dlURL) | ||
|
||
progress, _ := cmd.Flags().GetBool("progress") | ||
outPath, err := get.DownloadFileP(dlURL, progress) | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(outPath) | ||
|
||
fmt.Printf("Downloaded to: %s\n", outPath) | ||
|
||
f, err := os.OpenFile(outPath, os.O_RDONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
fmt.Printf("Unpacking Caddy to: %s\n", path.Join(installPath, "caddy")) | ||
|
||
if err := archive.Untar(f, installPath, true, true); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters