Skip to content

Commit

Permalink
Update Command and Function description comments throughout the project.
Browse files Browse the repository at this point in the history
  • Loading branch information
SwayKh committed Sep 22, 2024
1 parent b5167ea commit 54f18fe
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func Init(configName string) error {
return nil
}

// Update the Init Directory variable in the config
func UpdateInit(configuration *config.AppConfig, configName string) error {
err := config.UpdateInitDirectory(configuration, configName)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/SwayKh/linksym/pkg/utils"
)

// Get the "LinkName" as an argument, which should be the path relative to the
// Init Directory, and Find the matching LinkName through the []Records in
// .linksym.yaml, UnLink it, and Remove from the []Records.
// Expects one argument, and throws error on multiple arguments provided
// Get the absolute path of "LinkName", which should be the path relative from
// the Init Directory, and Find the matching LinkName through the []Records in
// .linksym.yaml, UnLink it, and Remove from the []Records. Expects one
// argument, and throws error on multiple arguments provided
func Remove(configuration *config.AppConfig, args []string) error {
switch len(args) {
case 1:
Expand All @@ -26,6 +26,7 @@ func Remove(configuration *config.AppConfig, args []string) error {
} else if !linkPath.Exists {
return fmt.Errorf("File %s doesn't exist", linkPath.AbsPath)
}

// Since the "filename" of the record can be the same with a different file,
// just linked in separate directories, getting the filename and the above
// directory name should make the name unique enough to be checked in
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Run() error {
// Since the Init Command creates the config file, the LoadConfig function
// can't be called before handling the init subcommand.
// But Init function calls aliasPath, which requires HomeDirectory variable,
// and hence function SetupDirectories was split up
// and InitialiseHomePath needs be called before this.
cmd.InitFlag.Parse(os.Args[2:])
if flag.Arg(0) == "init" && !cmd.UpdateInitBool {
return cmd.Init(configName)
Expand Down
9 changes: 2 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"github.com/SwayKh/linksym/pkg/utils"
)

// This package should, create the default config with the initialised directory
// Read the config for every other command to work and use the variable vales
// Add data under the "Links" variable whenever a new config is add via the
// project.
// The config file can be either .json .ini .toml .yaml
// I think yaml is a good file format for this

type AppConfig struct {
InitDirectory string `yaml:"init_directory"`
Records []record `yaml:"records"`
Expand Down Expand Up @@ -52,6 +45,7 @@ func (c *AppConfig) RemoveRecord(name string) {
}
}

// Un-Alias all paths with ~ and $init_directory with full absolute paths
func UnAliasConfig(configuration *AppConfig) {
configuration.InitDirectory = utils.ExpandPath(configuration.InitDirectory)

Expand All @@ -62,6 +56,7 @@ func UnAliasConfig(configuration *AppConfig) {
}
}

// Alias all mentions of HomeDirectory and InitDirectory with ~ and $init_directory
func AliasConfig(configuration *AppConfig) {
configuration.InitDirectory = utils.AliasPath(configuration.InitDirectory, true)

Expand Down
8 changes: 5 additions & 3 deletions pkg/config/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Load the configuration from .linksym.yaml configuration file and unmarshall
// it into the global Configuration variable, and un-alias all paths
// it into the global Configuration variable, and return pointer to this struct
func LoadConfig(configPath string) (*AppConfig, error) {
// Check if config file exists
config, err := utils.GetFileInfo(configPath)
Expand Down Expand Up @@ -41,8 +41,7 @@ func LoadConfig(configPath string) (*AppConfig, error) {
return configuration, nil
}

// Write the Configuration struct data to .linksym.yaml file after aliasing all
// paths with ~ and $init_directory
// Write the Configuration struct data to .linksym.yaml file
func WriteConfig(configuration *AppConfig, configPath string) error {
data, err := yaml.Marshal(configuration)
if err != nil {
Expand Down Expand Up @@ -78,6 +77,9 @@ func InitialiseConfig(configPath string) error {
return nil
}

// Takes *AppConfig as argument, updates the init_directory variables with
// current directory while keeping the []Records intact. And Write config back
// to file.
func UpdateInitDirectory(configuration *AppConfig, configPath string) error {
InitDirectory, err := os.Getwd()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/linker/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func MoveAndLink(sourcePath, destinationPath string, isDirectory bool) error {
return nil
}

// Create a symlink of source path at the destination path, and create a record
// of it
// Create a symlink of source path at the destination path,
func Link(sourcePath, destinationPath string) error {
err := os.Symlink(destinationPath, sourcePath)
if err != nil {
Expand All @@ -40,7 +39,7 @@ func Link(sourcePath, destinationPath string) error {
}

// Remove the symlink file at the source, move the destination file to the
// original source path. Undoing the MoveAndLink function basically
// original source path. Basically undo-ing the MoveAndLink function
func UnLink(sourcePath, destinationPath string, isDirectory bool) error {
err := deleteFile(sourcePath)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ func AliasPath(path string, skipInitDir bool) string {
return path
}

// Set values to global variables of InitDirectory and ConfigPath
func SetupDirectories(initDir string, configName string) {
global.InitDirectory = ExpandPath(initDir)
global.ConfigPath = filepath.Join(global.InitDirectory, configName)
}

// Set the global HomeDirectory variable. Separated from SetupDirectories to be
// used with the Init Subcommand.
func InitialiseHomePath() error {
var err error
global.HomeDirectory, err = os.UserHomeDir()
Expand Down

0 comments on commit 54f18fe

Please sign in to comment.