-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c31e43
commit bf30c9c
Showing
6 changed files
with
135 additions
and
0 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
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,73 @@ | ||
package rules | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
|
||
"github.com/doron-cohen/antidot/internal/tui" | ||
"github.com/doron-cohen/antidot/internal/utils" | ||
) | ||
|
||
type Export struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
func (e Export) Apply() error { | ||
envFile, err := utils.AppDirs.GetDataFile("env.sh") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !utils.FileExists(envFile) { | ||
if _, err = os.Create(envFile); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
envMap, err := godotenv.Read(envFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
existingValue, isKeyContained := envMap[e.Key] | ||
if isKeyContained { | ||
log.Printf("Key %s already exists in env file %s", e.Key, envFile) | ||
if existingValue != e.Value { | ||
errMessage := fmt.Sprintf( | ||
"Current value for key '%s' (%s) is different than the requested (%s)", | ||
e.Key, existingValue, e.Value, | ||
) | ||
return errors.New(errMessage) | ||
} else { | ||
return nil | ||
} | ||
} else { | ||
envMap[e.Key] = e.Value | ||
} | ||
|
||
log.Printf("Writing to %s", envFile) | ||
if err = utils.WriteEnvToFile(envMap, envFile); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e Export) Pprint() { | ||
log.Printf( | ||
" %s %s%s\"%s\"", | ||
tui.ApplyStyle(tui.Blue, "EXPORT"), | ||
e.Key, | ||
tui.ApplyStyle(tui.Gray, "="), | ||
e.Value, | ||
) | ||
} | ||
|
||
func init() { | ||
registerAction("export", Export{}) | ||
} |
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,35 @@ | ||
package utils | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/adrg/xdg" | ||
) | ||
|
||
type appDirs struct { | ||
AppName string | ||
} | ||
|
||
func (a appDirs) ConfigHome() string { | ||
return filepath.Join(xdg.ConfigHome, a.AppName) | ||
} | ||
|
||
func (a appDirs) CacheHome() string { | ||
return filepath.Join(xdg.CacheHome, a.AppName) | ||
} | ||
|
||
func (a appDirs) DataHome() string { | ||
return filepath.Join(xdg.DataHome, a.AppName) | ||
} | ||
|
||
func (a appDirs) GetDataFile(fileName string) (string, error) { | ||
relPath := filepath.Join(a.AppName, fileName) | ||
return xdg.DataFile(relPath) | ||
} | ||
|
||
var AppDirs appDirs | ||
|
||
func init() { | ||
// TODO: import this from somewhere | ||
AppDirs = appDirs{AppName: "antidot"} | ||
} |
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