Skip to content

Commit

Permalink
Merge pull request #1 from eddyv73/feature/refactor-dirs
Browse files Browse the repository at this point in the history
Feature/refactor dirs
  • Loading branch information
eddyv73 authored Jul 28, 2023
2 parents 3e5c9d4 + ade707e commit 6a705c8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 63 deletions.
25 changes: 25 additions & 0 deletions KubeConfigList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// KubeConfigList.swift
// kubec-cmd
//
// Created by Eddy Wister on 28/07/23.
//

import Foundation

func listfilesinpath () -> [String]
{
let fileManager = FileManager.default
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: kubeconfig.path.description)!
var ConfigFound = [String]()
for file in enumerator {
if let file = file as? String {
if file.contains(configsuffix) {
if !file.contains("bk") && !file.contains(".back") && file.contains(configsuffix) {
ConfigFound.append(file)
}
}
}
}
return ConfigFound
}
4 changes: 4 additions & 0 deletions kubec-cmd.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
2F2D0CB729319FE0005FDD2F /* Dirhelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F2D0CB629319FE0005FDD2F /* Dirhelper.swift */; };
2F2D0CB92931B341005FDD2F /* ArgsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F2D0CB82931B341005FDD2F /* ArgsManager.swift */; };
2F2D0CBB2931C2D7005FDD2F /* FilesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F2D0CBA2931C2D7005FDD2F /* FilesManager.swift */; };
2FA295C62A7460480002F584 /* KubeConfigList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FA295C52A7460480002F584 /* KubeConfigList.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -51,6 +52,7 @@
2F2D0CB629319FE0005FDD2F /* Dirhelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Dirhelper.swift; path = "kubec-cmd/Dirhelper.swift"; sourceTree = "<group>"; };
2F2D0CB82931B341005FDD2F /* ArgsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgsManager.swift; sourceTree = "<group>"; };
2F2D0CBA2931C2D7005FDD2F /* FilesManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilesManager.swift; sourceTree = "<group>"; };
2FA295C52A7460480002F584 /* KubeConfigList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KubeConfigList.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -88,6 +90,7 @@
2F03066D291B0A7E00F3C62C = {
isa = PBXGroup;
children = (
2FA295C52A7460480002F584 /* KubeConfigList.swift */,
2F2D0CB629319FE0005FDD2F /* Dirhelper.swift */,
2F030678291B0A7E00F3C62C /* kubec-cmd */,
2F030677291B0A7E00F3C62C /* Products */,
Expand Down Expand Up @@ -287,6 +290,7 @@
files = (
2F2D0CB92931B341005FDD2F /* ArgsManager.swift in Sources */,
2F03067A291B0A7E00F3C62C /* main.swift in Sources */,
2FA295C62A7460480002F584 /* KubeConfigList.swift in Sources */,
2F2D0CB729319FE0005FDD2F /* Dirhelper.swift in Sources */,
2F2D0CBB2931C2D7005FDD2F /* FilesManager.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "19"
endingLineNumber = "19"
landmarkName = "SearchFiles(target:)"
landmarkName = "SearchFiles(target:context:)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "28873129-027B-4C39-8135-CFE772E41BB4"
shouldBeEnabled = "Yes"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "kubec-cmd/FilesManager.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "101"
endingLineNumber = "101"
landmarkName = "listfilesinpath()"
landmarkName = "SwitcherConfig()"
landmarkType = "9">
<Locations>
<Location
Expand Down Expand Up @@ -159,6 +159,21 @@
endingLineNumber = "101"
offsetFromSymbolStart = "608">
</Location>
<Location
uuid = "28873129-027B-4C39-8135-CFE772E41BB4 - 4a9c5016e4236e7"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "kubec_cmd.SwitcherConfig() -&gt; ()"
moduleName = "kubec-cmd"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/eddywister/Projects/Repos/kubec-cmd/kubec-cmd/FilesManager.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "102"
endingLineNumber = "102"
offsetFromSymbolStart = "2856">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
Expand All @@ -174,8 +189,8 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "105"
endingLineNumber = "105"
landmarkName = "listfilesinpath()"
landmarkType = "9">
landmarkName = "unknown"
landmarkType = "0">
<Locations>
<Location
uuid = "F8ADF6F4-B23C-4743-B4EF-5DC8F07A900A - 9f0e14c3649430b3"
Expand Down
13 changes: 9 additions & 4 deletions kubec-cmd/ArgsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ func ArgsController() -> Args {
if let targetIndex = arguments.firstIndex(of: "-t"), arguments.count > targetIndex + 1 {
let targetFile = arguments[targetIndex + 1]
args.target = targetFile
print("Argument 1 ↪︎ ", arguments[targetIndex])
print("Argument 2 ↪︎ ", arguments[targetIndex + 1])
//only for debug
// print("Argument 1 ↪︎ ", arguments[targetIndex])
// print("Argument 2 ↪︎ ", arguments[targetIndex + 1])
print("Target found ➥", targetFile)
SearchFiles(target: args.target, context: args.context)
}
//new arg --list using this method listfilesinpath()
else if arguments.contains("--list")
{
let files = listfilesinpath()
print("List of files in path: ",kubeconfig)
print("List of files in path: ",kubeconfig.path.description)
for file in files {
print(file)
}
Expand All @@ -39,3 +40,7 @@ func ArgsController() -> Args {

return args
}




2 changes: 2 additions & 0 deletions kubec-cmd/Dirhelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ func PrintInstructions() {
print("🛠️---------------------------------------------------------------------------------------------------🛠️")
print("Using a specific context 📝: kubec-cmd -t 'subfix' -c 'context'")
print("🛠️---------------------------------------------------------------------------------------------------🛠️")
print("List config files 📝: kubec-cmd --list")
print("🛠️---------------------------------------------------------------------------------------------------🛠️")
}

49 changes: 21 additions & 28 deletions kubec-cmd/FilesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

import Foundation

let dir = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".kube").appendingPathComponent("bk")
var _target = ""
var _context = ""

func SearchFiles(target: String, context: String) {
_target = target
_context = context
CreateBackUpDirectory()
Makebackup()
getconfig()
Clean()
SwitcherConfig()

Expand All @@ -23,12 +20,26 @@ func SearchFiles(target: String, context: String) {
func CreateBackUpDirectory(){
//create directory
do {
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.createDirectory(at: dirbk, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription);
}
}

func getconfig ()
{
// list of string
//Select confifiles
for file in enumerator {
if let file = file as? String {
if file.contains(configsuffix) {
if !file.contains("bk") && !file.contains(".back")
{
ConfigFound.append(file)
}
}
}
}
}
func Makebackup() {
//date forma date + time
let date = Date()
Expand All @@ -39,10 +50,10 @@ func Makebackup() {
//copy files .kube to .kube/bk and append _date + time
for file in ConfigFound {
let source = kubeconfig.appendingPathComponent(file)
let destination = dir.appendingPathComponent(file).appendingPathExtension(result)
let destination = dirbk.appendingPathComponent(file).appendingPathExtension(result)
do {
try FileManager.default.copyItem(at: source, to: destination)
} catch let error as NSError {
} catch _ as NSError {
print("Error on Create backup ⚠️")
}
}
Expand All @@ -60,7 +71,7 @@ func Clean () {
if exist{
do {
try FileManager.default.removeItem(at: kubeconfigDir)
} catch let error as NSError {
} catch _ as NSError {
print("Error on Create backup ⚠️")
}
}
Expand All @@ -77,7 +88,7 @@ func SwitcherConfig() {
}

if existTarget {
let targetfile = "config_"+_target
let targetfile = configsuffix+_target
let source = kubeconfig.appendingPathComponent(targetfile)
let destination = kubeconfigDir
do {
Expand All @@ -103,21 +114,3 @@ func SwitcherConfig() {
}
}

//list all config files in path
func listfilesinpath () -> [String]
{
let home = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".kube").path();
let fileManager = FileManager.default
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: home)!
var ConfigFound = [String]()
for file in enumerator {
if let file = file as? String {
if file.contains(configsuffix) {
if !file.contains("bk") && !file.contains(".back") && file.contains("config_") {
ConfigFound.append(file)
}
}
}
}
return ConfigFound
}
32 changes: 6 additions & 26 deletions kubec-cmd/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,15 @@ import Foundation
let configsuffix = "config_"
let kubeconfigDir = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".kube").appendingPathComponent("config")
let kubeconfig = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".kube")

let dirbk = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".kube").appendingPathComponent("bk")
var _target = ""
var _context = ""
var ConfigFound = [String]()
let fileManager = FileManager.default
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: kubeconfig.path())!
PrintInstructions()

// Modified to handle both target and context arguments
let args = ArgsController()
let target = args.target
let context = args.context

if target.isEmpty {
print("Fail")
}

//get files in path
let fileManager = FileManager.default
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: kubeconfig.path())!

// list of string
var ConfigFound = [String]()
//Select confifiles
for file in enumerator {
if let file = file as? String {
if file.contains(configsuffix) {
if !file.contains("bk") && !file.contains(".back")
{
ConfigFound.append(file)
}
}
}
}

// Modified to handle context argument
SearchFiles(target: target, context: context)

0 comments on commit 6a705c8

Please sign in to comment.