mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 04:46:42 -04:00
Address JetBrains warnings
This commit is contained in:
+6
-8
@@ -14,8 +14,8 @@ import (
|
||||
"github.com/rwinkhart/libmutton/synccommon"
|
||||
)
|
||||
|
||||
// AgeEntry creates updates the age file for a vanity path.
|
||||
func AgeEntry(vanityPath string, timestamp int64) error {
|
||||
// Entry creates updates the age file for a vanity path.
|
||||
func Entry(vanityPath string, timestamp int64) error {
|
||||
ageFilePath := global.AgeDir + global.PathSeparator + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
f, err := os.OpenFile(ageFilePath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
@@ -29,10 +29,10 @@ func AgeEntry(vanityPath string, timestamp int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AgeAllPasswordEntries adds age data for all un-aged entries containing passwords.
|
||||
// AllPasswordEntries adds age data for all un-aged entries containing passwords.
|
||||
// Each entry is aged with a random timestamp from within the last year to prevent
|
||||
// all entries having their passwords expire at the same time.
|
||||
func AgeAllPasswordEntries(forceReage bool) error {
|
||||
func AllPasswordEntries(forceReage bool) error {
|
||||
allVanityPaths, _, err := synccommon.WalkEntryDir()
|
||||
if err != nil {
|
||||
return errors.New("unable to walk entry directory: " + err.Error())
|
||||
@@ -57,7 +57,7 @@ func AgeAllPasswordEntries(forceReage bool) error {
|
||||
// calculate random UNIX timestamp from within the last 365 days
|
||||
offsetInt, _ := rand.Int(rand.Reader, big.NewInt(31557600))
|
||||
randomOffset := time.Duration(offsetInt.Int64()) * time.Second
|
||||
err = AgeEntry(vanityPath, time.Now().Add(-randomOffset).Unix())
|
||||
err = Entry(vanityPath, time.Now().Add(-randomOffset).Unix())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -76,13 +76,11 @@ func TranslateAgeTimestamp(timestamp int64) uint8 {
|
||||
if timestamp == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
daysOld := time.Since(time.Unix(timestamp, 0)).Hours() / 24
|
||||
if daysOld >= 365 {
|
||||
return 3 // expired
|
||||
} else if daysOld >= 335 {
|
||||
return 2 // expiring soon
|
||||
} else {
|
||||
return 1 // fresh
|
||||
}
|
||||
return 1 // fresh
|
||||
}
|
||||
|
||||
+4
-4
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/rwinkhart/libmutton/global"
|
||||
)
|
||||
|
||||
type CfgT struct {
|
||||
type ConfigT struct {
|
||||
Libmutton struct {
|
||||
OfflineMode *bool `json:"offlineMode"`
|
||||
SSHUser *string `json:"sshUser"`
|
||||
@@ -25,12 +25,12 @@ type CfgT struct {
|
||||
}
|
||||
|
||||
// LoadConfig loads libmuttoncfg.json and returns the configuration.
|
||||
func LoadConfig() (*CfgT, error) {
|
||||
func LoadConfig() (*ConfigT, error) {
|
||||
cfgBytes, err := os.ReadFile(global.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to load libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
var cfg CfgT
|
||||
var cfg ConfigT
|
||||
err = json.Unmarshal(cfgBytes, &cfg)
|
||||
if err != nil {
|
||||
return nil, errors.New("unable to unmarshal libmuttoncfg.json: " + err.Error())
|
||||
@@ -41,7 +41,7 @@ func LoadConfig() (*CfgT, error) {
|
||||
// WriteConfig writes cfg to libmuttoncfg.json.
|
||||
// If used in append mode, any nil values in the
|
||||
// input cfg will be substituted with the existing values.
|
||||
func WriteConfig(cfg *CfgT, appendMode bool) error {
|
||||
func WriteConfig(cfg *ConfigT, appendMode bool) error {
|
||||
if appendMode {
|
||||
// check if any fields are nil
|
||||
var hasNilFields bool
|
||||
|
||||
+4
-7
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/rwinkhart/libmutton/crypt"
|
||||
)
|
||||
|
||||
// CopyShortcut, given a path, decrypts an
|
||||
// CopyShortcut (given a path) decrypts an
|
||||
// entry and copies a field to the clipboard.
|
||||
func CopyShortcut(realPath string, field int) error {
|
||||
// ensure realPath exists and is a file
|
||||
@@ -40,9 +40,6 @@ func CopyShortcut(realPath string, field int) error {
|
||||
if err != nil { // handle error from first copy
|
||||
return errors.New("error encountered in TOTP refresh process: " + err.Error())
|
||||
}
|
||||
if field != -1 {
|
||||
fmt.Println(back.AnsiGreen + "[Started]" + back.AnsiReset + " TOTP clipboard refresher\n\nService will run until this process is killed")
|
||||
}
|
||||
select {} // block indefinitely
|
||||
} else { // other
|
||||
// copy field to clipboard; launch clipboard clearing process
|
||||
@@ -57,12 +54,12 @@ func CopyShortcut(realPath string, field int) error {
|
||||
}
|
||||
}
|
||||
|
||||
// ClipClearArgument reads the assigned clipboard contents from stdin and passes them to clipClearProcess.
|
||||
func ClipClearArgument() error {
|
||||
// ClearArgument reads the assigned clipboard contents from stdin and passes them to clipClearProcess.
|
||||
func ClearArgument() error {
|
||||
assignedContents := back.ReadFromStdin()
|
||||
if assignedContents == "" {
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
err := ClipClearProcess(assignedContents)
|
||||
err := ClearProcess(assignedContents)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
"github.com/rwinkhart/go-boilerplate/back"
|
||||
)
|
||||
|
||||
// ClipClearProcess clears the clipboard after 30 seconds if the clipboard contents have not changed.
|
||||
// ClearProcess clears the clipboard after 30 seconds if the clipboard contents have not changed.
|
||||
// assignedContents can be omitted to clear the clipboard immediately and unconditionally.
|
||||
func ClipClearProcess(assignedContents string) error {
|
||||
func ClearProcess(assignedContents string) error {
|
||||
cmdPaste, cmdClear := getClipCommands()
|
||||
|
||||
clearClipboard := func() error {
|
||||
@@ -42,8 +42,7 @@ func ClipClearProcess(assignedContents string) error {
|
||||
}
|
||||
|
||||
if assignedContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||
err := clearClipboard()
|
||||
if err != nil {
|
||||
if err = clearClipboard(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -26,9 +26,8 @@ func GetOldEntryData(realPath string, field int) ([]string, error) {
|
||||
// return the old entry data with all required lines present
|
||||
if field > 0 {
|
||||
return ensureSliceLength(decryptedEntry, field), nil
|
||||
} else {
|
||||
return decryptedEntry, nil
|
||||
}
|
||||
return decryptedEntry, nil
|
||||
}
|
||||
|
||||
// ensureSliceLength is a utility function that ensures a slice is long enough to contain the specified index.
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ import (
|
||||
// rcwPassword and clientSpecificIniData can be left blank if not needed.
|
||||
func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData map[string]any, rcwPassword []byte, preserveOldConfigDir bool, forceOfflineMode bool) error {
|
||||
// handle clientSpecificIniData
|
||||
newCfg := &cfg.CfgT{}
|
||||
newCfg := &cfg.ConfigT{}
|
||||
if clientSpecificIniData != nil {
|
||||
newThirdPartyMap := make(map[string]any)
|
||||
maps.Copy(newThirdPartyMap, clientSpecificIniData)
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ func WriteEntry(realPath string, decSlice []string, passwordIsNew bool) error {
|
||||
if decSlice != nil {
|
||||
if passwordIsNew { // update age data when password changes
|
||||
if decSlice[0] != "" { // if the password change was NOT a removal, update the age file
|
||||
err = age.AgeEntry(global.GetVanityPath(realPath), time.Now().Unix())
|
||||
err = age.Entry(global.GetVanityPath(realPath), time.Now().Unix())
|
||||
if err != nil {
|
||||
return errors.New("unable to update age data: " + err.Error())
|
||||
}
|
||||
|
||||
+11
-13
@@ -27,29 +27,29 @@ import (
|
||||
// Only supports key-based authentication (passwords are supported for CLI-based implementations).
|
||||
func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
|
||||
// get SSH config info
|
||||
cfg, err := cfg.LoadConfig()
|
||||
config, err := cfg.LoadConfig()
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to parse SSH config: " + err.Error())
|
||||
}
|
||||
if *cfg.Libmutton.OfflineMode {
|
||||
if *config.Libmutton.OfflineMode {
|
||||
return nil, true, nil, nil, nil, nil
|
||||
}
|
||||
|
||||
// read private key
|
||||
key, err := os.ReadFile(*cfg.Libmutton.SSHKeyPath)
|
||||
key, err := os.ReadFile(*config.Libmutton.SSHKeyPath)
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to read private key: " + *cfg.Libmutton.SSHKeyPath)
|
||||
return nil, false, nil, nil, nil, errors.New("unable to read private key: " + *config.Libmutton.SSHKeyPath)
|
||||
}
|
||||
|
||||
// parse private key
|
||||
var parsedKey ssh.Signer
|
||||
if !*cfg.Libmutton.SSHKeyProtected {
|
||||
if !*config.Libmutton.SSHKeyProtected {
|
||||
parsedKey, err = ssh.ParsePrivateKey(key)
|
||||
} else {
|
||||
parsedKey, err = ssh.ParsePrivateKeyWithPassphrase(key, global.GetPassword("Enter password for your SSH keyfile:"))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to parse private key: " + *cfg.Libmutton.SSHKeyPath)
|
||||
return nil, false, nil, nil, nil, errors.New("unable to parse private key: " + *config.Libmutton.SSHKeyPath)
|
||||
}
|
||||
|
||||
// read known hosts file
|
||||
@@ -61,7 +61,7 @@ func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
|
||||
|
||||
// configure SSH client
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: *cfg.Libmutton.SSHUser,
|
||||
User: *config.Libmutton.SSHUser,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.PublicKeys(parsedKey),
|
||||
},
|
||||
@@ -70,12 +70,12 @@ func GetSSHClient() (*ssh.Client, bool, *bool, *string, *string, error) {
|
||||
}
|
||||
|
||||
// connect to SSH server
|
||||
sshClient, err := ssh.Dial("tcp", *cfg.Libmutton.SSHIP+":"+*cfg.Libmutton.SSHPort, sshConfig)
|
||||
sshClient, err := ssh.Dial("tcp", *config.Libmutton.SSHIP+":"+*config.Libmutton.SSHPort, sshConfig)
|
||||
if err != nil {
|
||||
return nil, false, nil, nil, nil, errors.New("unable to connect to remote server: " + err.Error())
|
||||
}
|
||||
|
||||
return sshClient, false, cfg.Libmutton.SSHIsWindows, cfg.Libmutton.SSHEntryRootPath, cfg.Libmutton.SSHAgeDirPath, nil
|
||||
return sshClient, false, config.Libmutton.SSHIsWindows, config.Libmutton.SSHEntryRootPath, config.Libmutton.SSHAgeDirPath, nil
|
||||
}
|
||||
|
||||
// GetSSHOutput runs a command over SSH and returns the output as a string.
|
||||
@@ -171,18 +171,16 @@ func getLocalData() (map[string]int64, error) {
|
||||
func getRealPathSFTP(vanityPath, serverEntryRoot string, serverIsWindows bool) string {
|
||||
if !serverIsWindows {
|
||||
return serverEntryRoot + vanityPath
|
||||
} else {
|
||||
return serverEntryRoot + strings.ReplaceAll(vanityPath, "/", "\\")
|
||||
}
|
||||
return serverEntryRoot + strings.ReplaceAll(vanityPath, "/", "\\")
|
||||
}
|
||||
|
||||
// getRealPathSFTP formats the vanityPath to match the remote server's entry/age file directory and path separator.
|
||||
func getRealAgePathSFTP(vanityPath, serverAgeDir string, serverIsWindows bool) string {
|
||||
if !serverIsWindows {
|
||||
return serverAgeDir + "/" + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
} else {
|
||||
return serverAgeDir + "\\" + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
}
|
||||
return serverAgeDir + "\\" + strings.ReplaceAll(vanityPath, "/", global.FSPath)
|
||||
}
|
||||
|
||||
// sftpSync takes two slices of entries (one for downloads and one for uploads) and syncs them between the client and server using SFTP.
|
||||
|
||||
@@ -68,7 +68,7 @@ end:
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenameRemoteFromClient renames oldVanityPath to newVanityPath on
|
||||
// RenameRemote renames oldVanityPath to newVanityPath on
|
||||
// the local system and calls the server to perform the rename remotely and add the
|
||||
// old target to the deletions list.
|
||||
// It can safely be called in offline mode, as well, so this is the intended
|
||||
@@ -120,7 +120,7 @@ end:
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddFolderRemoteFromClient creates a new entry-containing directory
|
||||
// AddFolderRemote creates a new entry-containing directory
|
||||
// on the local system and calls the server to create the folder remotely.
|
||||
// It can safely be called in offline mode, as well, so this is the
|
||||
// intended interface for adding folders (AddFolderLocal should only be
|
||||
|
||||
+1
-2
@@ -24,9 +24,8 @@ func WalkEntryDir() ([]string, []string, error) {
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("entry directory does not exist; initialize libmutton to create it")
|
||||
} else {
|
||||
return errors.New("an unexpected error occurred while generating the entry list: " + err.Error())
|
||||
}
|
||||
return errors.New("an unexpected error occurred while generating the entry list: " + err.Error())
|
||||
}
|
||||
|
||||
// trim root path from each path before storing and replace backslashes with forward slashes
|
||||
|
||||
Reference in New Issue
Block a user