-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement vagrant global-status machine detection
- Loading branch information
1 parent
ed684e6
commit e580f27
Showing
4 changed files
with
100 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// VagrantGlobalStatusScanner.h | ||
// Vagrant Manager | ||
// | ||
// Copyright (c) 2014 Lanayo. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface VagrantGlobalStatusScanner : NSObject | ||
|
||
- (NSArray*)getInstancePaths; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// VagrantGlobalStatusScanner.m | ||
// Vagrant Manager | ||
// | ||
// Copyright (c) 2014 Lanayo. All rights reserved. | ||
// | ||
|
||
#import "VagrantGlobalStatusScanner.h" | ||
|
||
@implementation VagrantGlobalStatusScanner | ||
|
||
//find vagrant instances listed in the vagrant global-status command output | ||
- (NSArray*)getInstancePaths { | ||
NSMutableArray *paths = [[NSMutableArray alloc] init]; | ||
|
||
//get output of vagrant global-status | ||
NSTask *task = [[NSTask alloc] init]; | ||
[task setLaunchPath:@"/bin/bash"]; | ||
[task setArguments:@[@"-c", @"vagrant global-status"]]; | ||
|
||
NSPipe *pipe = [NSPipe pipe]; | ||
[task setStandardInput:[NSPipe pipe]]; | ||
[task setStandardOutput:pipe]; | ||
|
||
[task launch]; | ||
[task waitUntilExit]; | ||
|
||
if(task.terminationStatus == 0) { | ||
//parse instance info from global-status output | ||
NSData *outputData = [[pipe fileHandleForReading] readDataToEndOfFile]; | ||
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding]; | ||
|
||
//search for machine state in output string | ||
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\/.*)(\\n|$)" options:0 error:NULL]; | ||
NSArray *matches = [regex matchesInString:outputString options:0 range:NSMakeRange(0, [outputString length])]; | ||
for(NSTextCheckingResult *match in matches) { | ||
//NSRange idRange = [match rangeAtIndex:1]; | ||
//NSRange nameRange = [match rangeAtIndex:2]; | ||
//NSRange providerRange = [match rangeAtIndex:3]; | ||
//NSRange stateRange = [match rangeAtIndex:4]; | ||
NSRange pathRange = [match rangeAtIndex:5]; | ||
|
||
NSString *path = [[outputString substringWithRange:pathRange] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | ||
|
||
BOOL vagrantFileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString pathWithComponents:@[path, @"/Vagrantfile"]]]; | ||
|
||
if(vagrantFileExists) { | ||
[paths addObject:path]; | ||
} | ||
} | ||
} | ||
|
||
return [NSArray arrayWithArray:paths]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters