@@ -24,6 +24,10 @@ var confList = []*Config{
2424 shellName : "zsh" ,
2525 rcFile : ".zshrc" ,
2626 },
27+ {
28+ shellName : "fish" ,
29+ rcFile : ".config/fish/config.fish" ,
30+ },
2731}
2832
2933func (c * Config ) GetRCFilePath () (string , error ) {
@@ -32,6 +36,17 @@ func (c *Config) GetRCFilePath() (string, error) {
3236 return "" , err
3337 }
3438 rcFilePath := filepath .Join (home , c .rcFile )
39+
40+ // For fish, ensure the directory exists
41+ if c .shellName == "fish" {
42+ configDir := filepath .Dir (rcFilePath )
43+ if ! fileutil .FolderExists (configDir ) {
44+ if err := os .MkdirAll (configDir , os .ModePerm ); err != nil {
45+ return "" , fmt .Errorf ("failed to create fish config directory %v got %v" , configDir , err )
46+ }
47+ }
48+ }
49+
3550 if fileutil .FileExists (rcFilePath ) {
3651 return rcFilePath , nil
3752 }
@@ -79,7 +94,13 @@ func add(path string) (bool, error) {
7994 return false , errorutil .NewWithErr (err ).Msgf ("add %s to $PATH env" , path )
8095 }
8196
82- script := fmt .Sprintf ("export PATH=$PATH:%s\n \n " , path )
97+ var script string
98+ if conf .shellName == "fish" {
99+ script = fmt .Sprintf ("fish_add_path %s\n \n " , path )
100+ } else {
101+ script = fmt .Sprintf ("export PATH=$PATH:%s\n \n " , path )
102+ }
103+
83104 return exportToConfig (conf , path , script )
84105}
85106
@@ -93,8 +114,15 @@ func remove(path string) (bool, error) {
93114 if err != nil {
94115 return false , errorutil .NewWithErr (err ).Msgf ("remove %s from $PATH env" , path )
95116 }
96- pathVars = sliceutil .PruneEqual (pathVars , path )
97- script := fmt .Sprintf ("export PATH=%s\n \n " , strings .Join (pathVars , ":" ))
117+
118+ var script string
119+ if conf .shellName == "fish" {
120+ script = fmt .Sprintf ("set --erase fish_user_paths[contains $fish_user_paths %s]\n \n " , path )
121+ } else {
122+ pathVars = sliceutil .PruneEqual (pathVars , path )
123+ script = fmt .Sprintf ("export PATH=%s\n \n " , strings .Join (pathVars , ":" ))
124+ }
125+
98126 return exportToConfig (conf , path , script )
99127}
100128
@@ -115,21 +143,31 @@ func exportToConfig(config *Config, path, script string) (bool, error) {
115143 lines := strings .Split (strings .TrimSpace (string (b )), "\n " )
116144 for _ , line := range lines {
117145 if strings .EqualFold (line , strings .TrimSpace (script )) {
118- gologger .Info ().Msgf ("Run `source ~/%s` to add %s to $PATH " , config .rcFile , path )
146+ if config .shellName == "fish" {
147+ gologger .Info ().Msgf ("Run `source %s` to add %s to $PATH " , rcFilePath , path )
148+ } else {
149+ gologger .Info ().Msgf ("Run `source ~/%s` to add %s to $PATH " , config .rcFile , path )
150+ }
119151 return true , nil
120152 }
121153 }
122154 f , err := os .OpenFile (rcFilePath , os .O_APPEND | os .O_WRONLY , 0644 )
123155 if err != nil {
124156 return false , err
125157 }
158+
126159 script = fmt .Sprintf ("\n \n # Generated for pdtm. Do not edit.\n %s" , script )
127160 if _ , err := f .Write ([]byte (script )); err != nil {
128161 return false , err
129162 }
130163 if err := f .Close (); err != nil {
131164 return false , err
132165 }
133- gologger .Info ().Label ("WRN" ).Msgf ("Run `source ~/%s` to add $PATH (%s)" , config .rcFile , path )
166+
167+ if config .shellName == "fish" {
168+ gologger .Info ().Label ("WRN" ).Msgf ("Run `source %s` to add $PATH (%s)" , rcFilePath , path )
169+ } else {
170+ gologger .Info ().Label ("WRN" ).Msgf ("Run `source ~/%s` to add $PATH (%s)" , config .rcFile , path )
171+ }
134172 return true , nil
135173}
0 commit comments