Skip to content

Allow explicit command aliasing for the CLU CLI #1592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/CLU/Microsoft.CLU.Common/CLUEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public static class CLUEnvironment
/// </summary>
private static string PackagesRootPathEnvVaribleName = "PackagesRootPath";

/// <summary>
/// Name of the environment variable holding working directory root path.
/// </summary>
private static string WorkDirsRootPathEnvVaribleName = "WorkDirsRootPath";

/// <summary>
/// Set the root paths.
/// </summary>
Expand All @@ -41,7 +36,6 @@ public static void SetRootPaths(string cluRootPath)
rootPathPrefix = "/";
}
SetEnvironmentVariable(PackagesRootPathEnvVaribleName, rootPathPrefix + Path.Combine(cluRootPath, Common.Constants.PackageFolderName));
SetEnvironmentVariable(WorkDirsRootPathEnvVaribleName, Path.Combine(cluRootPath, Common.Constants.WorkFolderName));
}

/// <summary>
Expand Down
87 changes: 8 additions & 79 deletions src/CLU/Microsoft.CLU.Common/CmdletIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,28 @@ namespace Microsoft.CLU
///
/// [_indexes]
/// |
/// ===========================================
/// | | |
/// | | |
/// [vm] [network] _cmdlet.idx
/// | |
/// ================== [virtual]
/// | | | |
/// [create] [delete] _cmdlet.idx ==================
/// | | | | |
/// _cmdlet.idx _cmdlet.idx [create] [delete] _cmdlet.idx
/// | |
/// _cmdlet.idx _cmdlet.idx
///
/// _cmdlet.idx
///
/// The notation [abc] represents a directory, where 'abc' is the directory name.
///
/// [_indexes] represents the root directory "_indexes".
/// There is a directory corrosponding to each discriminator of commands, each such directory contains a
/// file named _cmdlet.idx which is the index for commands this discriminator participate.
/// For example _cmdlet.idx under [virtual] diretory will contains index for following commands
/// vm network create
/// vm network delete
/// _cmdlet.idx under [virtual]/[create] index
/// vm network create
/// </summary>
internal class CmdletIndex
{
/// <summary>
/// Name of the index.
/// </summary>
public string Name { get; set; }

/// <summary>
/// The index-entries. These entries will be serialized to/from the file _cmdlet.idx.
/// </summary>
public ConfigurationDictionary Entries { get; set; } = new ConfigurationDictionary();

/// <summary>
/// Child indicies.
/// </summary>
public Dictionary<string, CmdletIndex> Children { get; set; } = new Dictionary<string, CmdletIndex>();

/// <summary>
/// Index a command identified by the given command discriminators.
/// </summary>
/// <param name="keys">The command discriminator list from which key needs to be derived</param>
/// <param name="value">The value</param>
public void Add(List<string> keys, string value)
{
Add(keys, 0, value);
}

/// <summary>
/// Recursively index each discriminator in the given command keys.
/// </summary>
/// <param name="keys">The command discriminator list from which key needs to be derived</param>
/// <param name="level">The discriminator level</param>
/// <param name="value">The value</param>
private void Add(List<string> keys, int level, string value)
private void Add(List<string> keys, string value)
{
Entries.Add(string.Join(Constants.CmdletIndexWordSeparator, keys), value);
if (level < keys.Count)
{
CmdletIndex childIndex;
if (!Children.TryGetValue(keys[level], out childIndex))
{
childIndex = new CmdletIndex { Name = keys[level] };
Children.Add(keys[level], childIndex);
}

childIndex.Add(keys, level + 1, value);
}
}

/// <summary>
Expand All @@ -105,51 +56,31 @@ public void Save(string path)
{
Debug.Assert(!string.IsNullOrEmpty(path));

var fullPath = string.IsNullOrEmpty(Name) ? path : Path.Combine(path, Name);
if (!Directory.Exists(fullPath))
Directory.CreateDirectory(fullPath);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

var indexFilePath = Path.Combine(fullPath, Constants.CmdletsIndexFileName);
var indexFilePath = Path.Combine(path, Constants.CmdletsIndexFileName);
if (Entries.Count > 0)
Entries.Store(indexFilePath);

foreach (var idx in Children.Values)
{
idx.Save(fullPath);
}
}

/// <summary>
/// Load the index.
/// </summary>
/// <param name="keys">The command discriminator list identifying the index to be loaded</param>
/// <param name="path">The index file path</param>
/// <param name="recursive">
/// True - If the index for all command discriminator in the discriminator list needs to be loaded
/// False - Only the index of the root discriminator needs to be loaded</param>
/// <returns></returns>
public static CmdletIndex Load(IEnumerable<string> keys, string path, bool recursive)
public static CmdletIndex Load(string path)
{
Debug.Assert(keys != null);
Debug.Assert(!string.IsNullOrEmpty(path));

var indexName = keys.FirstOrDefault();
var indexFullPath = Path.Combine(path, Constants.CmdletsIndexFileName);

var cmdletIndex = new CmdletIndex { Name = indexName };
var cmdletIndex = new CmdletIndex();
if (File.Exists(indexFullPath))
{
cmdletIndex.Entries = ConfigurationDictionary.Load(indexFullPath);
}

if (recursive)
{
foreach (var dir in Directory.EnumerateDirectories(path))
{
cmdletIndex.Children.Add(indexName, Load(keys.Skip(1), Path.Combine(path, indexName), true));
}
}

return cmdletIndex;
}

Expand All @@ -158,9 +89,7 @@ public static CmdletIndex Load(IEnumerable<string> keys, string path, bool recur
/// </summary>
public void Clear()
{
Name = null;
Entries.Clear();
Children.Clear();
}
}
}
13 changes: 2 additions & 11 deletions src/CLU/Microsoft.CLU.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ public static class Constants
/// </summary>
public const string CLUConfigFileName = "msclu.cfg";

/// <summary>
/// The standard name of a command index file on disk.
/// </summary>
internal const string StaticCommandIndex = "_index";

/// <summary>
/// The standard name of a cmdlet index file on disk.
/// </summary>
internal const string CmdletsIndexFileName = "_cmdlets.idx";

#if PSCMDLET_HELP
/// <summary>
/// The standard name of a name mapping file on disk.
/// </summary>
internal const string NameMappingFileName = "_namemap.idx";

#endif
/// <summary>
/// The name of a package's index folder.
/// </summary>
Expand All @@ -56,11 +52,6 @@ public static class Constants
/// </summary>
public const string PackageFolderName = "pkgs";

/// <summary>
/// The canonical name of the root folder for all command package working directories.
/// </summary>
public const string WorkFolderName = "workdirs";

/// <summary>
/// Package installation marker file name.
/// </summary>
Expand Down
Loading