Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3196

basename() convenience function

    XMLWordPrintable

    Details

    • Complexity:
      S

      Description

      Currently (as of 3.14.x) I can use string_split() and nth() or data_regextract(), or most similarly lastnode() to pick apart various parts of a file path.

      For example:

      bundle agent main
      {
        vars:
            "path" string => "/etc/path/to/something/kewl.conf";
      
            "file"
              data => data_regextract( "(?<directory>.*/)(?<basename>.*)$", $(path) );
      
            "filename"
              string => lastnode( $(path), $(const.dirsep) );
      }
      
      # cf-agent --no-lock --log-level info --show-evaluated-vars=default:main\\. --file example.cf
      Variable name                            Variable value                                               Meta tags                               
      default:main.file                        {"0":"/etc/path/to/something/kewl.conf","basename":"kewl.conf","directory":"/etc/path/to/something/"} source=promise                          
      default:main.path                        /etc/path/to/something/kewl.conf                             source=promise                          
      default:main.filename                kewl.conf                                                                         source=promise                          
      

      It would be convenient and familiar to have functions like basename() and dirname() from coreutils.

      bundle agent main
      {
        vars:
            "path" string => "/etc/path/to/something/kewl.conf";
      
            "basename_of_path"
              string => basename( $(path) );
      
            "basename_of_path_wo_suffix"
              string => basename( $(path), ".conf" );
      
            "dirname_of_path"
              string => basename( $(path), ".conf" );
      
      }
      
      # cf-agent --no-lock --log-level info --show-evaluated-vars=default:main\\. --file example.cf
      Variable name                            Variable value                                               Meta tags                               
      default:main.path                        /etc/path/to/something/kewl.conf                             source=promise                          
      default:main.basename_of_path            kewl.conf                                                    source=promise                          
      default:main.basename_of_path_wo_suffix  kewl                                                         source=promise                          
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                karlhto Karl Hole Totland
                Reporter:
                a10042 Nick Anderson
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Summary Panel