Skip to content

Commit

Permalink
Add Alpacon logo at the start of a websh session
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Mar 26, 2024
1 parent 81f8676 commit ed9bd9d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
3 changes: 3 additions & 0 deletions api/websh/websh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/alpacanetworks/alpacon-cli/api/server"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"github.com/gorilla/websocket"
"golang.org/x/term"
"io"
Expand Down Expand Up @@ -76,6 +77,8 @@ func OpenNewTerminal(ac *client.AlpaconClient, sessionResponse SessionResponse)
}

func websocketClient(conn *websocket.Conn) error {
utils.ShowLogo()

if !term.IsTerminal(int(os.Stdin.Fd())) {
return errors.New("websh command should be a terminal")
}
Expand Down
16 changes: 1 addition & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,13 @@ import (
"github.com/spf13/cobra"
)

func showLogo() {
alpaconLogo := `
(` + "`" + `-') _ _ (` + "`" + `-') (` + "`" + `-') _ <-. (` + "`" + `-')_
(OO ).-/ <-. \-.(OO ) (OO ).-/ _ .-> \( OO) )
/ ,---. ,--. ) _.' \ / ,---. \-,-----.(` + "`" + `-')----. ,--./ ,--/
| \ /` + ".`" + `\ | (` + "`" + `-')(_...--'' | \ /` + ".`" + `\ | .--./( OO).-. '| \ | |
'-'|_.' | | |OO )| |_.' | '-'|_.' | /_) (` + "`" + `-')( _) | | || . '| |)
(| .-. |(| '__ || .___.'(| .-. | || |OO ) \| |)| || |\ |
| | | | | |'| | | | | |(_' '--'\ ' '-' '| | \ |
` + "`" + `--' ` + "`" + `--' ` + "`" + `-----' ` + "`" + `--' ` + "`" + `--' ` + "`" + `--' ` + "`" + `-----' ` + "`" + `-----' ` + "`" + `--' ` + "`" + `--'
`
fmt.Println(alpaconLogo)
}

var rootCmd = &cobra.Command{
Use: "alpacon",
Aliases: []string{"ac"},
Short: "Alpacon CLI: Your Gateway to Alpacon Services",
Long: "Use this tool to interact with the alpacon service.",
Run: func(cmd *cobra.Command, args []string) {
showLogo()
utils.ShowLogo()
fmt.Println("Welcome to Alpacon CLI! Use 'alpacon [command]' to execute a specific command or 'alpacon help' to see all available commands.")
},
}
Expand Down
41 changes: 41 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@ package utils

import (
"bufio"
"errors"
"fmt"
"github.com/google/uuid"
"golang.org/x/term"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)

func ShowLogo() {
alpaconLogo := `
(` + "`" + `-') _ _ (` + "`" + `-') (` + "`" + `-') _ <-. (` + "`" + `-')_
(OO ).-/ <-. \-.(OO ) (OO ).-/ _ .-> \( OO) )
/ ,---. ,--. ) _.' \ / ,---. \-,-----.(` + "`" + `-')----. ,--./ ,--/
| \ /` + ".`" + `\ | (` + "`" + `-')(_...--'' | \ /` + ".`" + `\ | .--./( OO).-. '| \ | |
'-'|_.' | | |OO )| |_.' | '-'|_.' | /_) (` + "`" + `-')( _) | | || . '| |)
(| .-. |(| '__ || .___.'(| .-. | || |OO ) \| |)| || |\ |
| | | | | |'| | | | | |(_' '--'\ ' '-' '| | \ |
` + "`" + `--' ` + "`" + `--' ` + "`" + `-----' ` + "`" + `--' ` + "`" + `--' ` + "`" + `--' ` + "`" + `-----' ` + "`" + `-----' ` + "`" + `--' ` + "`" + `--'
`
fmt.Println(alpaconLogo)
}

func ReadFileFromPath(filePath string) ([]byte, error) {
absolutePath, err := filepath.Abs(filePath)
if err != nil {
Expand Down Expand Up @@ -253,3 +269,28 @@ func IsUUID(str string) bool {
_, err := uuid.Parse(str)
return err == nil
}

func CreateAndEditTempFile(data []byte) (string, error) {
tmpfile, err := os.CreateTemp("", "example.*.json")
if err != nil {
return "", errors.New("failed to create temp file for update")
}
defer tmpfile.Close()

if _, err := tmpfile.Write(data); err != nil {
return "", err
}

if err := tmpfile.Close(); err != nil {
return "", err
}

cmd := exec.Command("vim", tmpfile.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return "", err
}

return tmpfile.Name(), nil
}

0 comments on commit ed9bd9d

Please sign in to comment.