PageRenderTime 27ms CodeModel.GetById 7ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/update-engine/Samples/EngineRunner/ERChangeTicketCommand.m

http://macfuse.googlecode.com/
Objective C | 99 lines | 54 code | 25 blank | 20 comment | 8 complexity | 5c489f01d45a6d676cd9d62b35c9ec1a MD5 | raw file
 1// Copyright 2008 Google Inc.
 2//
 3// Licensed under the Apache License, Version 2.0 (the "License");
 4// you may not use this file except in compliance with the License.
 5// You may obtain a copy of the License at
 6//
 7//     http://www.apache.org/licenses/LICENSE-2.0
 8//
 9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#import "ERChangeTicketCommand.h"
16
17#import "KSUpdateEngine.h"
18
19
20@implementation ERChangeTicketCommand
21
22- (NSString *)name {
23  return @"change";
24}  // name
25
26
27- (NSString *)blurb {
28  return @"Change attributes of a ticket";
29}  // blurb
30
31
32- (NSDictionary *)requiredArguments {
33  return [NSDictionary dictionaryWithObjectsAndKeys:
34                       @"Path to the ticket store", @"store",
35                       @"Product ID to change", @"productid",
36                       nil];
37
38}  // requiredArguments
39
40
41- (NSDictionary *)optionalArguments {
42  return [NSDictionary dictionaryWithObjectsAndKeys:
43                       @"New product version", @"version",
44                       @"New existence checker path", @"xcpath",
45                       @"New server URL", @"url",
46                       nil];
47
48}  // optionalArguments
49
50
51- (BOOL)runWithArguments:(NSDictionary *)args {
52
53  // Get ahold of the ticket store.
54  NSString *storePath = [args objectForKey:@"store"];
55  KSTicketStore *ticketStore = [KSTicketStore ticketStoreWithPath:storePath];
56
57  // Pull the ticket out for the particular product to change.
58  NSString *productID = [args objectForKey:@"productid"];
59  KSTicket *ticket = [ticketStore ticketForProductID:productID];
60
61  if (ticket == nil) {
62    fprintf(stderr, "Could not find product '%s' in ticket store '%s'",
63            [productID UTF8String], [storePath UTF8String]);
64    return NO;
65  }
66
67  // Tickets are immutable objects, so we will need to create a brand
68  // new ticket with the changes.  Use an old value if the corresponding
69  // optional parameter has not been provided.
70  NSString *version = [args objectForKey:@"version"];
71  if (version == nil) {
72    version = [ticket version];
73  }
74
75  KSExistenceChecker *existenceChecker = [ticket existenceChecker];
76  NSString *xcpath = [args objectForKey:@"xcpath"];
77  if (xcpath != nil) {
78    existenceChecker = [KSPathExistenceChecker checkerWithPath:xcpath];
79  }
80
81  NSURL *serverURL = [ticket serverURL];
82  NSString *urlString = [args objectForKey:@"url"];
83  if (urlString != nil) {
84    serverURL = [NSURL URLWithString:urlString];
85  }
86
87  // Make the new ticket and put it into the store.  This will replace
88  // the existing ticket, and write it out to disk.
89  KSTicket *newTicket = [KSTicket ticketWithProductID:productID
90                                              version:version
91                                     existenceChecker:existenceChecker
92                                            serverURL:serverURL];
93  BOOL result = [ticketStore storeTicket:newTicket];
94
95  return result;
96
97}  // runWithArguments
98
99@end  // ERChangeTicketCommand