// download each file and directory recursively from IFS private void GetDirectoryRecursively(KanpachiClient client, string serverPath, string outPath) { Console.WriteLine($"Downloading directory {serverPath} to {outPath}"); foreach (SftpFile f in client.ListDirectory(serverPath)) { if (f.Name != "." && f.Name != "..") { if (f.IsDirectory) { var outSubPath = Path.Combine(outPath, f.Name); if (!Directory.Exists(outSubPath)) { Directory.CreateDirectory(outSubPath); } GetDirectoryRecursively(client, f.FullName, outSubPath); } else { var outFile = Path.Combine(outPath, f.Name.Replace('/', Path.DirectorySeparatorChar)); Console.WriteLine($"Downloading file {f.FullName} to {outFile}"); File.WriteAllText(outFile, Encoding.UTF8.GetString(client.DownloadFile(f.FullName))); } } } }
public void ExecSql(string sqlString) { using (KanpachiClient client = new KanpachiClient(Profile)){ DataTable tbl = client.ExecSQL(sqlString); if (tbl.Rows.Count > 0) { List <string> cols = new List <string>(); foreach (DataColumn col in tbl.Columns) { cols.Add(col.ColumnName); } ConsoleTable ct = new ConsoleTable(cols.ToArray()); foreach (DataRow row in tbl.Rows) { ct.AddRow(row.ItemArray); } ct.Write(Format.Alternative); } else { Console.WriteLine("Statement executed successfully."); } } }
// Display list of libraries on active profile's host public void ListLibraries() { using (KanpachiClient client = new KanpachiClient(Profile)){ foreach (Library lib in client.GetLibraries()) { Console.WriteLine(lib); } } }
// Download a directory from IFS public void GetDirectory(string serverPath, string clientPath) { var outPath = ClientUtils.BuildDownloadPath(clientPath, Profile); using (KanpachiClient client = new KanpachiClient(Profile)){ Console.WriteLine($"Downloading {serverPath} to {clientPath}"); GetDirectoryRecursively(client, serverPath, outPath); } }
// download source member and write to local file private void DownloadSrcMbr(KanpachiClient client, string downloadPath, string lib, string spf, SrcMbr srcMbr) { var outPath = Path.Combine(BuildLocalQsysPath(downloadPath, lib, spf), $"{srcMbr.Name}.{srcMbr.Attribute}"); var qsysPath = $"/QSYS.LIB/{lib}.LIB/{spf}.FILE/{srcMbr.Name}.MBR"; WriteQsysMetadata(client, downloadPath, lib, spf, srcMbr); Console.WriteLine($"Downloading {qsysPath} to {outPath}"); srcMbr.Content = client.DownloadMember(qsysPath); File.WriteAllText(outPath, string.Join("\n", SplitSrcMbr(srcMbr))); }
// List contents of IFS directory public void ListDirectory(string serverPath) { var ifsPath = (serverPath == null || serverPath.Length == 0) ? Profile.IfsUserPath : serverPath; using (KanpachiClient client = new KanpachiClient(Profile)){ var entries = client.ListDirectory(ifsPath); foreach (SftpFile entry in client.ListDirectory(ifsPath)) { Console.WriteLine(entry.FullName); } } }
public void ExecShell(string shellString) { using (KanpachiClient client = new KanpachiClient(Profile)){ var cmd = client.ExecShell(shellString); if (cmd.ExitCode != 0) { throw new KanpachiShellException( $"Command\n\t{cmd.CmdText}\n completed with exit code {cmd.ExitCode}\n\t{cmd.StdErr}"); } Console.WriteLine(cmd.StdOut); } }
// Display list of source physical files public void ListSpfs(string qsysPath) { if (!RegexUtils.MatchIbmiObject(qsysPath)) { throw new KanpachiFormatException("Expected name of library."); } using (KanpachiClient client = new KanpachiClient(Profile)){ foreach (SrcPf spf in client.GetSrcPfList(qsysPath)) { Console.WriteLine(spf); } } }
// Download file from IFS public void GetFile(string serverPath, string clientPath) { var fileName = Path.GetFileName(serverPath); var outPath = ClientUtils.BuildDownloadPath(clientPath, Profile); if (!Path.HasExtension(outPath)) { outPath = Path.Combine(outPath, fileName); } using (KanpachiClient client = new KanpachiClient(Profile)){ Console.WriteLine($"Downloading {serverPath} to {outPath}"); File.WriteAllText(outPath, Encoding.UTF8.GetString(client.DownloadFile(serverPath))); } }
// write QSYS metadata (TEXT, RECORDLEN, ATTRIBUTE, etc) private void WriteQsysMetadata(KanpachiClient client, string downloadPath, string lib, string spf, SrcMbr mbr) { Library library = null; var metadataPath = Path.Combine(ClientUtils.BuildDownloadPath(downloadPath, Profile), "QSYS", lib, $"{lib}.json"); if (File.Exists(metadataPath)) { // read existing metadata file for update using (StreamReader f = File.OpenText(metadataPath)){ library = (Library) new JsonSerializer().Deserialize(f, typeof(Library)); int spfIdx = library.SrcPfs.FindIndex(x => x.Name == spf); // source physical file not found in library metadata, so add it if (spfIdx == -1) { library.SrcPfs.Add(client.GetSrcPfDetails(lib, spf)); spfIdx = library.SrcPfs.Count - 1; } SrcPf srcPf = library.SrcPfs[spfIdx]; int mbrIdx = srcPf.Members.FindIndex(x => x.Name == mbr.Name); // source member not found in source physical file metadata, so add it if (mbrIdx == -1) { srcPf.Members.Add(mbr); mbrIdx = srcPf.Members.Count - 1; } library.SrcPfs[spfIdx].Members[mbrIdx] = mbr; } } else { // library doesn't exist in metadata, setup new one library = client.GetLibraryDetails(lib); SrcPf srcPf = client.GetSrcPfDetails(lib, spf); srcPf.Members.Add(mbr); library.SrcPfs.Add(srcPf); } // update metadata file using (StreamWriter f = File.CreateText(metadataPath)){ f.Write(JsonConvert.SerializeObject(library, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })); } }
// Display list of source members in LIB/SRCPF public void ListMembers(string qsysPath) { if (!RegexUtils.MatchSrcPfPath(qsysPath)) { throw new KanpachiFormatException("Expected source physical file path of format LIB/SRCPF."); } var splitPath = qsysPath.ToUpper().Split('/'); (string lib, string spf) = (splitPath[0], splitPath[1]); using (KanpachiClient client = new KanpachiClient(Profile)){ foreach (SrcMbr mbr in client.GetSrcMbrList(lib, spf)) { Console.WriteLine(mbr); } } }
// Download source member at LIB/SRCPF/MBR public void GetMember(string qsysPath, string downloadPath) { if (!RegexUtils.MatchSrcMbrPath(qsysPath)) { throw new KanpachiFormatException("Expected source member path of format LIB/SRCPF/MBR."); } var splitPath = qsysPath.ToUpper().Split('/'); (string lib, string spf, string mbr) = (splitPath[0], splitPath[1], splitPath[2]); var srcPath = $"/QSYS.LIB/{lib}.LIB/{spf}.FILE/{mbr}.MBR"; var clientPath = BuildLocalQsysPath(downloadPath, lib, spf); using (KanpachiClient client = new KanpachiClient(Profile)){ SrcMbr srcMbr = client.GetSrcMbrDetails(lib, spf, mbr); DownloadSrcMbr(client, downloadPath, lib, spf, srcMbr); } }
// Download entire library public void GetLibrary(string lib, string downloadPath) { if (!RegexUtils.MatchIbmiObject(lib)) { throw new KanpachiFormatException("Invalid library name"); } using (KanpachiClient client = new KanpachiClient(Profile)){ var spfs = client.GetSrcPfList(lib); foreach (SrcPf spf in spfs) { var members = client.GetSrcMbrList(lib, spf.Name); foreach (SrcMbr srcMbr in members) { // TODO: progress output DownloadSrcMbr(client, downloadPath, lib, spf.Name, srcMbr); } } } }
// Download source physical file at LIB/SRCPF public void GetSpf(string qsysPath, string downloadPath) { if (!RegexUtils.MatchSrcPfPath(qsysPath)) { throw new KanpachiFormatException("Invalid QSYS path. Expected source physical file path of format LIB/SRCPF."); } var splitPath = qsysPath.ToUpper().Split('/'); (string lib, string spf) = (splitPath[0], splitPath[1]); using (KanpachiClient client = new KanpachiClient(Profile)){ SrcPf srcPf = client.GetSrcPfDetails(lib, spf); var members = client.GetSrcMbrList(lib, spf); foreach (SrcMbr srcMbr in members) { // TODO: progress output DownloadSrcMbr(client, downloadPath, lib, spf, srcMbr); } } }