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

findfiles_up() : Policy function to find a file searching UP a tree

    XMLWordPrintable

    Details

    • Type: Story
    • Status: Done
    • Priority: Medium
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.18.0
    • Component/s: None
    • Labels:
    • Story Points:
      0

      Description

      It would be nifty if we had a function that could find a file UP from a given point in a directory structure.

      e.g. search_up( $(this.promise_dirname), ".git/config", inf );

      • User must provide a path to start searching from. This directory itself should be searched.
      • User must provide a path to search for (this path is relative to the currently searched directory
      • User can provide the number of levels to search, if not provided, it should search to the root of the file system (real inf, not CFEngine inf of 999999999).
      • When number of levels up to search is 0, the search is limited to the first directory.

      One place this would be immediately useful for me is for acceptance tests in core. Most acceptance tests include dcs.cf.sub via a relative path up from the current location. It's really annoying to get that path correct, it would be much easier to search_up( $(this.promise_dirname), "dcs.cf.sub", inf );

      I propose search_up because that is the hip way to refer to searching (based on my anecdotal evidence observing my own children 9 and 11 years old).
      https://george.mand.is/2017/02/why-do-kids-say-search-it-up/

      Bikesheding the search_up function

      search_up() searches up the directory tree structure for a file, .e.g. search_up("/tmp/deep/dir", ".git/config", inf) would search up the directory tree as many as inf levels (999999999) starting at /tmp/deep/dir, then /tmp/deep, then /tmp/ looking for .git/config. It should not be possible for the array to have more than the number of levels UP to search elements, nor should it be possible to have more than the number of parent directories until you reach the root of the file system. In this example case, no more than 999999999 elements in the array would be possible, though we hit the limit of the file system after searching 3 paths. The function should search across file system boundaries.

      Should search_up() return a data container, string, or list?

      • A string is very limited, getting us just one result, a list or a data container provide a bit more flexibility.
      • Data container has the benefit of being able to reference a position directly, e.g. $(dc[0]) for the first result where as a specific list element would need to be accessed with nth() list
      • A data container array and a list can both be iterated directly

      Given these considerations, a data container array seems to be the best choice.

      bundle agent __main__
      {
        vars:
            "dc" data => '["first", "second"]';
            "li" slist => { "1st", "2nd" };
            "big" int => "inf";
      
        reports:
          "CFEngine version $(sys.cf_version)"; 
          "$(dc[0])";
          "$(li[0])";
          "$(with)" with => nth(li, 0);
          "$(big)";
          "dc iter $(dc)";
          "li iter $(li)";
      }
      
      R: CFEngine version 3.17.0
      R: first
      R: $(li[0])
      R: 1st
      R: 999999999
      R: dc iter first
      R: dc iter second
      R: li iter 1st
      R: li iter 2nd

        Attachments

          Activity

            People

            • Assignee:
              larsewi Lars Erik Wik
              Reporter:
              a10042 Nick Anderson
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Summary Panel