afx icon indicating copy to clipboard operation
afx copied to clipboard

if `~/.config/afx` is a relative link, cannot execute the `afx` command

Open menghuu opened this issue 2 years ago • 10 comments

WHAT

if ~/.config/afx is a relative link, cannot execute the afx command

m ~/.config
❯ ls -la
total 20
drwxr-xr-x  5 m m 4096 May  3 20:29 .
drwxr-xr-x 22 m m 4096 May  3 20:29 ..
lrwxrwxrwx  1 m m   38 May  3 20:29 afx -> /home/m/projs/dotfiles/afx/.config/afx


m ~/.config
❯ afx
Package manager for CLI
...
...
Use "afx [command] --help" for more information about a command.

but, if ~/.config/afx is a relative link

❯ ls ~/.config/ -l
total 12
lrwxrwxrwx 1 m m   33 May  3 20:30 afx -> ../projs/dotfiles/afx/.config/afx

m ~/projs/dotfiles master ≡*+9 ~2 -1
❯ afx
[ERROR]: failed to initialize afx: /home/m/.config/afx: failed to walk dir: stat ../projs/dotfiles/afx/.config/afx: no such file or directory

WHY

menghuu avatar May 03 '22 12:05 menghuu

Which version did you use? Maybe this case has been fixed on v0.1.21 (#41)

babarot avatar May 04 '22 14:05 babarot

  • the version is 0.1.21
  • it works with regular dir/file
❯ ls -ld .config/afx
drwxr-xr-x 2 m m 4096 May  5 02:37 .config/afx
❯ afx --version  # it works
afx version 0.1.21 (v0.1.21/054f74c)
  • it also works with absolute link
❯ ls -la
total 24
drwxr-xr-x  6 m m 4096 May  5 02:52 .
drwxr-xr-x 24 m m 4096 May  5 02:51 ..
lrwxrwxrwx  1 m m   11 May  5 02:52 afx -> /home/m/afx
❯ afx --version
afx version 0.1.21 (v0.1.21/054f74c)
  • but, it not works with relative link
❯ ls ~/.config/afx -ld
lrwxrwxrwx 1 m m 34 May  5 02:41 /home/m/.config/afx -> ../projs/dotfiles/main/.config/afx
❯ afx --version # it not works
[ERROR]: failed to initialize afx: /home/m/.config/afx: failed to walk dir: stat ../projs/dotfiles/main/.config/afx: no such file or directory
❯ cat ~/.config/afx/main.yaml
github:
- name: stedolan/jq
  description: Command-line JSON processor
  owner: stedolan
  repo: jq
  release:
    name: jq
    tag: jq-1.6
  command:
    link:
    - from: '*jq*'
      to: jqm ~

@b4b4r07

menghuu avatar May 04 '22 18:05 menghuu

Thank you. Now fixed on new version.

babarot avatar May 04 '22 20:05 babarot

@b4b4r07

  • there is another problem: afx will fails if ~/.config/afx is not exist, Is it a design or a bug?
❯ afx --version
[ERROR]: failed to initialize afx: /home/m/.config/afx: failed to walk dir: lstat /home/m/.config/afx: no such file or directory
  • if the ~/.config/afx is a link(or more accuracy, a relative link?), afx will not install or update the package
❯ afx --version
afx version 0.1.22 (v0.1.22/4b34e4c)
❯ afx install
No packages to install
❯ afx update
No packages to update
❯ ls -ld ~/.config/afx
lrwxrwxrwx 1 m m 34 May  5 14:45 /home/m/.config/afx -> ../projs/dotfiles/main/.config/afx
❯ cat ~/.config/afx/main.yaml
github:
- name: stedolan/jq
  description: Command-line JSON processor
  owner: stedolan
  repo: jq
  release:
    name: jq
    tag: jq-1.6
  command:
    link:
    - from: '*jq*'
      to: jqm ~/projs/dotfiles
  • if ~/.config/afx is a regular dir, it works
❯ rm ~/.config/afx -fr
❯ mkdir ~/.config/afx
❯ cp main/.config/afx/main.yaml ~/.config/afx/
❯ afx --version
afx version 0.1.22 (v0.1.22/4b34e4c)
❯ afx install
? OK to install these packages? stedolan/jq No
Cancelled

menghuu avatar May 05 '22 06:05 menghuu

Okay,

let me think about this from the beginning...

if ~/.config/afx is a relative link, cannot execute the afx command

Why should I take care of this case? What kind of motivations or backgrounds do you have?

babarot avatar May 05 '22 07:05 babarot

em...

  • ~/projs/dotfiles is my dotfile repo
  • I use stow to link $HOME dotfiles towords my dotfile repo, stow creates the relative link
  • I want to add afx-package-infos in my dotfile repo, and stow will link ~/.config/afx to my dotfile repo's subdir main/.config/afx
  • if i am in a new system, i can stow -t ~ main and afx install to install packages

menghuu avatar May 05 '22 07:05 menghuu

I think using relative symlink is rarely in the first place. I'm getting the feeling like relative link itself is not good link and also should not support it in app.

This is because generally we should use readlink system call in order to resolve symbolic link but that API will return just a linked path whether its path is relative or not. In that case, it's difficult to resolve absolute path. I can construct an absolute path by using filepath.Join and filepath.Clean etc but also difficult to continue to do walk dir inside app .

What do you think about this?

babarot avatar May 05 '22 08:05 babarot

actually, I donot familiar with golang, but, if afx will suopprt (almost) unlimited dir level(with link), the simplest implement is: recurrence like this:

def to_absolute_path(filepath):
    if filepath is absolutepath:
        return filepath
    else:
        return to_abs(filepath)  # maybe using joining


def walk(absolute_filepath, reutrn_list, visited_list):
    # make sure: absolute_filepath is not visited
    # make sure: absolute_filepath is absolute path not link

    visited_list.append(absolute_filepath)

    if absolute_filepath is regulare_file:
        reutrn_list.append(absolute_filepath) # maybe check wheather it is a valid yaml
        return 
    else:
        for sub_filepath in absolute_filepath:
            sub_filepath = to_absolute_path(sub_filepath)
            
            if sub_filepath in visited_list:
                raise Exception('loop') # or just pass
            else:
                # sub_filepath is absolute path and not link 
                # sub_filepath is not visited
                walk(sub_filepath, reutrn_list=reutrn_list, visited_list=visited_list) 


config_root_dir_path = '/absolute/relative/or/regular/dir/path'
# make sure: config_root_dir_path is absolute path not link
config_root_dir_path = to_absolute_path(config_root_dir_path)

# make sure: config_root_dir_path is not visited
_visited_list = [] # a set

all_config_file_paths = [] # a set

walk(config_root_dir_path, reutrn_list=all_config_file_paths, visited_list=_visited_list)


menghuu avatar May 05 '22 13:05 menghuu

Of course we can implement this but what I wanted to say is whether I should support this case or not. Relative symbolic link is rare case, right?

babarot avatar May 05 '22 13:05 babarot

ok, It's up to you to decide

menghuu avatar May 05 '22 14:05 menghuu