Skip to content

Commit

Permalink
feat: init cmd for initializing antidot
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Oct 9, 2020
1 parent 975d27c commit 3e3f7b8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"log"

"github.com/spf13/cobra"

"github.com/doron-cohen/antidot/internal/utils"
)

func init() {
rootCmd.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize antidot for aliases and env vars to work",
Run: func(cmd *cobra.Command, args []string) {
// TODO: detect shell and generate appropriate script
envFilePath, err := utils.GetEnvFile()
if err != nil {
log.Fatalf("Failed to get env file path: %v", err)
}

aliasFilePath, err := utils.GetAliasFile()
if err != nil {
log.Fatalf("Failed to get alias file path: %v", err)
}

fmt.Printf(`if [[ "$ANTIDOT_INIT" != "1" ]]; then
%s
source %s
source %s
export ANTIDOT_INIT=1
fi`,
utils.IndentLines(utils.XdgVarsExport()),
envFilePath,
aliasFilePath,
)
},
}
11 changes: 11 additions & 0 deletions internal/utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/user"

"github.com/adrg/xdg"
"github.com/joho/godotenv"
)

Expand Down Expand Up @@ -33,6 +34,16 @@ func WriteEnvToFile(envMap map[string]string, filePath string) error {
return godotenv.Write(newMap, filePath)
}

func XdgVarsExport() string {
return fmt.Sprintf(`export XDG_CONFIG_HOME="%s"
export XDG_CACHE_HOME="%s"
export XDG_DATA_HOME="%s"`,
xdg.ConfigHome,
xdg.CacheHome,
xdg.DataHome,
)
}

func GetAliasFile() (string, error) {
return AppDirs.GetDataFile("alias.sh")
}
Expand Down
14 changes: 14 additions & 0 deletions internal/utils/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package utils

import (
"fmt"
"strings"
)

func IndentLines(lines string) string {
var builder strings.Builder
for _, line := range strings.Split(lines, "\n") {
builder.WriteString(fmt.Sprintf(" %s\n", line))
}
return builder.String()
}

0 comments on commit 3e3f7b8

Please sign in to comment.